packageme.http;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.io.OutputStream;importjava.io.OutputStreamWriter;importjava.net.HttpURLConnection;importjava.net.InetSocketAddress;importjava.net.Proxy;importjava.net.URL;importjava.net.URLConnection;importjava.util.Iterator;importjava.util.Map;public classHttpRequestor {private String charset = "utf-8";private Integer connectTimeout = null;private Integer socketTimeout = null;private String proxyHost = null;private Integer proxyPort = null;/*** Do GET request
*@paramurl
*@return*@throwsException
*@throwsIOException*/
public String doGet(String url) throwsException {
URL localURL= newURL(url);
URLConnection connection= this.openConnection(localURL);
HttpURLConnection httpURLConnection=(HttpURLConnection)connection;
httpURLConnection.setRequestProperty("Accept-Charset", charset);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
InputStream inputStream= null;
InputStreamReader inputStreamReader= null;
BufferedReader reader= null;
StringBuffer resultBuffer= newStringBuffer();
String tempLine= null;//响应失败
if (httpURLConnection.getResponseCode() >= 300) {throw new Exception("HTTP Request is not success, Response code is " +httpURLConnection.getResponseCode());
}try{
inputStream=httpURLConnection.getInputStream();
inputStreamReader= newInputStreamReader(inputStream);
reader= newBufferedReader(inputStreamReader);while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
}finally{if (reader != null) {
reader.close();
}if (inputStreamReader != null) {
inputStreamReader.close();
}if (inputStream != null) {
inputStream.close();
}
}returnresultBuffer.toString();
}/*** Do POST request
*@paramurl
*@paramparameterMap
*@return*@throwsException*/
public String doPost(String url, Map parameterMap) throwsException {/*Translate parameter map to parameter date string*/StringBuffer parameterBuffer= newStringBuffer();if (parameterMap != null) {
Iterator iterator=parameterMap.keySet().iterator();
String key= null;
String value= null;while(iterator.hasNext()) {
key=(String)iterator.next();if (parameterMap.get(key) != null) {
value=(String)parameterMap.get(key);
}else{
value= "";
}
parameterBuffer.append(key).append("=").append(value);if(iterator.hasNext()) {
parameterBuffer.append("&");
}
}
}
System.out.println("POST parameter : " +parameterBuffer.toString());
URL localURL= newURL(url);
URLConnection connection= this.openConnection(localURL);
HttpURLConnection httpURLConnection=(HttpURLConnection)connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept-Charset", charset);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterBuffer.length()));
OutputStream outputStream= null;
OutputStreamWriter outputStreamWriter= null;
InputStream inputStream= null;
InputStreamReader inputStreamReader= null;
BufferedReader reader= null;
StringBuffer resultBuffer= newStringBuffer();
String tempLine= null;try{
outputStream=httpURLConnection.getOutputStream();
outputStreamWriter= newOutputStreamWriter(outputStream);
outputStreamWriter.write(parameterBuffer.toString());
outputStreamWriter.flush();//响应失败
if (httpURLConnection.getResponseCode() >= 300) {throw new Exception("HTTP Request is not success, Response code is " +httpURLConnection.getResponseCode());
}//接收响应流
inputStream =httpURLConnection.getInputStream();
inputStreamReader= newInputStreamReader(inputStream);
reader= newBufferedReader(inputStreamReader);while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
}finally{if (outputStreamWriter != null) {
outputStreamWriter.close();
}if (outputStream != null) {
outputStream.close();
}if (reader != null) {
reader.close();
}if (inputStreamReader != null) {
inputStreamReader.close();
}if (inputStream != null) {
inputStream.close();
}
}returnresultBuffer.toString();
}private URLConnection openConnection(URL localURL) throwsIOException {
URLConnection connection;if (proxyHost != null && proxyPort != null) {
Proxy proxy= new Proxy(Proxy.Type.HTTP, newInetSocketAddress(proxyHost, proxyPort));
connection=localURL.openConnection(proxy);
}else{
connection=localURL.openConnection();
}returnconnection;
}/*** Render request according setting
*@paramrequest*/
private voidrenderRequest(URLConnection connection) {if (connectTimeout != null) {
connection.setConnectTimeout(connectTimeout);
}if (socketTimeout != null) {
connection.setReadTimeout(socketTimeout);
}
}/** Getter & Setter*/
publicInteger getConnectTimeout() {returnconnectTimeout;
}public voidsetConnectTimeout(Integer connectTimeout) {this.connectTimeout =connectTimeout;
}publicInteger getSocketTimeout() {returnsocketTimeout;
}public voidsetSocketTimeout(Integer socketTimeout) {this.socketTimeout =socketTimeout;
}publicString getProxyHost() {returnproxyHost;
}public voidsetProxyHost(String proxyHost) {this.proxyHost =proxyHost;
}publicInteger getProxyPort() {returnproxyPort;
}public voidsetProxyPort(Integer proxyPort) {this.proxyPort =proxyPort;
}publicString getCharset() {returncharset;
}public voidsetCharset(String charset) {this.charset =charset;
}
}