📄 head.java
字号:
package com.mindprod.http;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
/**
* simulates a browser posting a form to CGI via HEAD .
* It does not read any data. It is used for link checking where the server accepts HEAD requests.
* If it ignores HEAD requests, use Probe instead.
*
* @author Roedy Green, Canadian Mind Products may be copied and used freely for any purpose but military.
* @version 1.9 2008-08-22 support accept-charset, accept-encoding and accept-language. Fix bugs in gzip support.
*/
@SuppressWarnings( { "WeakerAccess" } )
public final class Head extends Http
{
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* constructor
*/
public Head()
{
}
/**
* Check if a URL is working.
*
* @param url complete URL including get parms
* only http:
* @return responseCode
*/
@SuppressWarnings( { "UnusedAssignment", "MethodNamesDifferingOnlyByCase" } )
public int head( URL url )
{
try
{
// defaults
responseCode = DEFAULT_RESPONSE_CODE;
responseMessage = DEFAULT_RESPONSE_MESSAGE;
// O P E N
final HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection();
urlc.setAllowUserInteraction( false );
urlc.setDoInput( true );
urlc.setDoOutput( false );// nothing beyond original request
urlc.setUseCaches( false );
urlc.setRequestMethod( "HEAD" );
setStandardProperties( urlc );
urlc.connect();
/* save responseCode for later retrieval */
responseCode = urlc.getResponseCode();
responseMessage = urlc.getResponseMessage();
urlc.disconnect();
return responseCode;
}
catch ( ClassCastException e )
{
// was not an http: url
return responseCode;
}
catch ( IOException e )
{
return responseCode;
}
}// end probe
/**
* Send a form full of data to the CGI host using GET.
*
* @param host host name of the website, Should be form:"www.mindprod.com", no lead http://.
* @param port -1 if default, 8081 for local echoserver.
* @param action action of form, page on website. Usually has a lead /.
* @param parms alternating parm value without = or ? not urlEncoded.
* @return responseCode
*/
@SuppressWarnings( { "UnusedAssignment", "MethodNamesDifferingOnlyByCase" } )
public int head( String host,
int port,
String action,
String... parms )
{
try
{
// defaults
responseCode = DEFAULT_RESPONSE_CODE;
responseMessage = DEFAULT_RESPONSE_MESSAGE;
// O P E N
final StringBuilder sb = new StringBuilder( 200 );
for ( int i = 0; i < parms.length - 1; i += 2 )
{
if ( i != 0 )
{
sb.append( "&" );
}
sb.append( URLEncoder.encode( parms[ i ], "UTF-8"
/* encoding */ ) );
sb.append( '=' );
sb.append( URLEncoder.encode( parms[ i + 1 ], "UTF-8"
/* encoding */ ) );
}
final String encodedParms = sb.toString();
// O P E N
// URL will encode target and parms.
URL url = new URI( "http",
null,
host,
port,
action,
null
/* no query here, or would be double encoded */,
null ).toURL();
url = new URL( url.toString() + '?' + encodedParms );
return head( url );
}
catch ( URISyntaxException e )
{
return responseCode;
}
catch ( IOException e )
{
return responseCode;
}
}// end probe
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -