📄 http.java
字号:
// Originally based on work by Jonathan Revusky
// To encode strings use java.net.URLEncoder.encode;
// and Java.net.URLDecoder.decode or Request.
/*
* copyright (c) 1998-2008 Canadian Mind Products
* Roedy Green
* Canadian Mind Products
* #327 - 964 Heywood Avenue
* Victoria, BC Canada V8V 2Y5
* tel: (250) 361-9093
* roedy g at mindprod dotcom
* http://mindprod.com
* 1.1 2007-07-19 improved handling of responseCode
* 1.2 2007-07-27 use UTF-8 instead of 8859_1.
* 1.3 2007-08-24 readStringBlocking, readBytesBlocking, encoding on Get
* 1.4 2007-09-26 add TIMEOUT
* 1.5 2007-12-30 add alternate get and post methods that take a full URL.
* 1.6 2008-01-14 add gzip option on read
* 1.7 2008-07-25 add configurable User-Agent, add Base Http class.
* 1.8 2008-07-27 handle case where URL given was not HTTP
* 1.9 2008-08-22 support accept-charset, accept-encoding and accept-language. Fix bugs in gzip support.
*/
package com.mindprod.http;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URLConnection;
/**
* base class to Post, Get, Probe
*
* @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.
*/
abstract class Http
{
// ------------------------------ FIELDS ------------------------------
/**
* true if want extra debugging ouput
*/
static final boolean DEBUGGING = false;
/**
* responseCode to give if is no proper one
*/
static final int DEFAULT_RESPONSE_CODE = -1;
/**
* responseMessage to give if is no proper one. Might mean tried to use http: on https: URL.
*/
static final String DEFAULT_RESPONSE_MESSAGE = "no connect";
/**
* the referer we pretend to be.
* By default null, for none.
*/
String referer = null;
/**
* responseCode in words from most recent post
*/
String responseMessage;
/**
* the browser we pretend to be
*/
String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
/**
* Allow 50 seconds to connect
*/
int connectTimeout = 50 * 1000;
/**
* Allow 40 seconds for a read to go without progress
*/
int readTimeout = 40 * 1000;
/**
* responseCode from most recent post
*/
int responseCode;
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* responseCode from most recent post/get
*
* @return responseCode
*/
public int getResponseCode()
{
return responseCode;
}
/**
* responseCode from most recent post/get
*
* @return responseCode
*/
public String getResponseMessage()
{
return responseMessage;
}
/**
* override the default connect timeout of 50 seconds
*
* @param connectTimeout timeout to connect in ms. Note int not long.
*/
public void setConnectTimeout( int connectTimeout )
{
this.connectTimeout = connectTimeout;
}
/**
* override the default read timeout of 40 seconds
*
* @param readTimeout timeout to connect int ms. Note int not long.
*/
public void setReadTimeout( int readTimeout )
{
this.readTimeout = readTimeout;
}
/**
* set theRefer ie. the name of a web page this request ostensibly came from.
*
* @param referer e.g "http://mindprod.com/index.html", null for none.
* @see <a href="http://mindprod.com/jgloss/http.html">details on Referer</a>
*/
public void setReferer( String referer )
{
this.referer = referer;
}
/**
* override the default User-Agent
*
* @param userAgent User-Agent string a browser uses in an HTTP header to identify itself.
* null for no User Agent. By default you get Firefox.
* @see <a href="http://mindprod.com/jgloss/http.html">details on User-Agent</a>
*/
public void setUserAgent( String userAgent )
{
this.userAgent = userAgent;
}
// --------------------------- CONSTRUCTORS ---------------------------
/**
* no public instantiation. Just a base class.
*/
Http()
{
}
// -------------------------- OTHER METHODS --------------------------
/**
* process the response from the request we sent the server
*
* @param encoding Encoding to use to interpret the result.
* @param urlc the HttpURLConnection, all ready to go but for the connect.
* @return content of the response, decompressed, decoded.
* @throws java.io.IOException if trouble reading the stream.
*/
String processResponse( String encoding, HttpURLConnection urlc )
throws IOException
{
urlc.connect();
/* save responseCode for later retrieval */
responseCode = urlc.getResponseCode();
responseMessage = urlc.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;
}
// InputStream gives us the raw bytes. We must decompress and decode the 8-bit chars..
// actually a sun.net.www.protocol.http.HttpURLConnection.HttpInputStream
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. We don't handle deflate or Unix compress.
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( "ResponseCode:" + responseCode );
System.out.println( "ResponseMessage:" + responseMessage );
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();
urlc.disconnect();
return result;
}
/**
* set up the standard properties on the connection
*
* @param urlc Connection we are setting up.
*/
protected void setStandardProperties( URLConnection urlc )
{
urlc.setConnectTimeout( connectTimeout );
urlc.setReadTimeout( readTimeout );
if ( userAgent != null )
{
urlc.setRequestProperty( "user-agent", userAgent );
}
if ( referer != null )
{
urlc.setRequestProperty( "referer", referer );
}
urlc.setRequestProperty( "accept",
"text/html, image/png, image/jpeg, image/gif, application/x-java-serialized-object, text/x-java-source, text/xml, application/xml, "
+
"text/css, application/x-java-jnlp-file, text/plain, application/zip, application/octet-stream, *; q=.2, */*; q=.2" );
urlc.setRequestProperty( "accept-charset", "iso-8859-1, utf-8, utf-16, *;q=0.1" );
// no deflate.
urlc.setRequestProperty( "accept-encoding", "gzip, x-gzip, identity, *;q=0" );
// relaxed, prefer English
urlc.setRequestProperty( "accept-language", "en-CA,en;q=0.9" );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -