http://blog.csdn.net/wangpeng047/article/details/19624529
http://blog.csdn.net/zknxx/article/details/51598852
一、所需jar包
HttpClient 是apache 組織下面的一個用於處理HTTP 請求和響應的開源工具。所需jar包:
commons-logging-1.1.1.jar、httpclient-4.5.2.jar、httpcore-4.4.4.jar
二、使用方法
使用HttpClient發送請求、接收響應很簡單,一般需要如下幾步即可。
1. 創建HttpClient對象。
2. 創建請求方法的實例,並指定請求URL。如果需要發送GET請求,創建HttpGet對象;如果需要發送POST請求,創建HttpPost對象。
3. 如果需要發送請求參數,可調用HttpGet、HttpPost共同的setParams(HetpParams params)【過時,不推薦使用】方法來添加請求參數;對於HttpPost對象而言,也可調用setEntity(HttpEntity entity)方法來設置請求參數。對於HttpGet對象,可通過拼接url來添加請求參數。
4. 調用HttpClient對象的execute(HttpUriRequest request)發送請求,該方法返回一個HttpResponse。
5. 調用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可獲取服務器的響應頭;
調用HttpResponse的getEntity()方法可獲取HttpEntity對象,該對象包裝了服務器的響應內容。程序可通過該對象獲取服務器的響應內容。
6. 釋放連接。無論執行方法是否成功,都必須釋放連接
三、示例
package com.learn.http;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpSend {
public static void main(String[] args) {
// post請求百度地址,返回404
new HttpSend().post("http://www.baidu.com");
new HttpSend().get("http://www.baidu.com");
}
/**
* 發送 post請求訪問本地應用並根據傳遞參數不同返回不同結果
*/
public void post(String url) {
// 創建默認的httpClient實例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 創建httppost
HttpPost httppost = new HttpPost(url);
// 創建參數隊列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
// 添加參數
// formparams.add(new BasicNameValuePair("gg", "1"));
UrlEncodedFormEntity uefEntity;
try {
//傳入空參數,創建實體對象
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
//設置post對象的實體對象
httppost.setEntity(uefEntity);
System.out.println("executing post request " + httppost.getURI());
//執行post請求
CloseableHttpResponse response = httpclient.execute(httppost);
try {
//獲取請求結果對象
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("--------------------------------------");
System.out.println("Response content: " + EntityUtils.toString(entity));
System.out.println("--------------------------------------");
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關閉連接,釋放資源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 發送 get請求
*/
public void get(String url) {
// 創建默認的httpClient實例.
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// 封裝請求參數
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("cityEname", "henan"));
//轉換爲鍵值對
String str = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
System.out.println(str);
//創建帶參數get請求(示例)
HttpGet httpGet = new HttpGet(url+"?"+str);
// 創建不帶參數get請求
HttpGet httpget = new HttpGet(url);
System.out.println("executing get request " + httpget.getURI());
// 執行get請求.
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 獲取響應實體
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印響應狀態
System.out.println(response.getStatusLine());
if (entity != null) {
// 打印響應內容長度
System.out.println("Response content length: " + entity.getContentLength());
// 打印響應內容
System.out.println("Response content: " + EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 關閉連接,釋放資源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}