java java.net.url_通过java.net.URLConnection发送HTTP请求的方法

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值