📄 fetch.java
字号:
package com.mindprod.http;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* Reads with a generic http: https: file: etc, arbitrary URL
*
* @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 Fetch extends Http
{
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* constructor
*/
public Fetch()
{
}
/**
* Read a message given an URL. Does not support getResponseCode or getResponseMessage.
*
* @param url complete URL including any parms. Use Request.appendCGIPair to build up parms on the tail end.
* might be http: https: file: and possibly others.
* @param encoding encoding of the byte stream result, usually UTF-8 or or ISO-8859-1.
* @return host's response with headers and embedded length fields stripped.
* @see "com.mindprod.filetransfer.Download"
*/
@SuppressWarnings( { "UnusedAssignment", "MethodNamesDifferingOnlyByCase" } )
public String fetch( URL url, String encoding )
{
try
{
// defaults
responseCode = DEFAULT_RESPONSE_CODE;
responseMessage = DEFAULT_RESPONSE_MESSAGE;
// O P E N
final URLConnection urlc = url.openConnection();
// Not actually connecting yet, just getting connection object,
// urlc will contain subclasses of URLConnection like:
// http: HttpURLConnection
// https: HttpsURLConnectionImpl
// file: FileURLConnection
urlc.setAllowUserInteraction( false );
urlc.setDoInput( true );
urlc.setDoOutput( false );// nothing beyond original request
urlc.setUseCaches( false );
// we leave it up to URLConection to figure out the request method.
setStandardProperties( urlc );
urlc.connect();
// urlConnection does not support getResponseCode or getResponseMessage
// get size of message. -1 means comes in an indeterminate number of chunks.
int estimatedLength = urlc.getContentLength();
if ( estimatedLength < 0 )
{
// quite common for no length field
estimatedLength = 32 * 1024;
}
final InputStream is = urlc.getInputStream();
final String contentType = urlc.getContentType();
// Content-Type: text/html; charset=utf-8
int place = contentType.lastIndexOf( "charset=" );
final String revisedEncoding;
if ( place >= 0 )
{
revisedEncoding = contentType.substring( place + "charset=".length() ).trim().toUpperCase();
}
else
{
revisedEncoding = encoding;
}
// content encoding might be null
final boolean gzipped = "gzip".equals( urlc.getContentEncoding() )
|| "x-gzip".equals( urlc.getContentEncoding() );
// R E A D
String result = Read.readStringBlocking( is,
estimatedLength,
readTimeout,
gzipped,
revisedEncoding );
if ( DEBUGGING )
{
System.out.println( "--------------------------------" );
System.out.println( "ContentType:" + contentType );
System.out.println( "CharSet:" + revisedEncoding );
System.out.println( "ContentEncoding:" + urlc.getContentEncoding() );
System.out.println( "Result:" + ( result == null ? "null" : result.substring( 0, Math.min( result.length(), 300 ) ) ) );
}
// C L O S E
is.close();
// There is no corresponding URLConnection.disconnect
return result;
}
catch ( IOException e )
{
return null;
}
}// end fetch
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -