開始之前的準備工作:
1、建立測試響應數據所需的服務項目ServerProject(實際開發中是請求網絡服務器),提供服務,並啓動
服務的的controller代碼:
@RequestMapping("/query")
public void query(@RequestParam("name")String username) throws Exception {
//doing business......
resp.getWriter().println("Welcome "+username+"!,"+"start doing Service ......ing");
}
2、創建一個項目ClientProject用於測試請求
開始使用
第一步: ClientProject添加HttpComponents的Maven依賴
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<!-- StringEntity設置傳輸參數時,使用fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
參考地址:添加Maven依賴官方參考地址
httpclient : 官方文檔
幾個相關項目(具體使用文檔參考上面的官方文檔連接):
HttpClient Cache : 提供了緩存的api
HttpMime :文件上傳下載操作api
Fluent HC :官方基於流式,封裝了httpclient的api,簡化操作
第二步: 編寫測試代碼
public class HttpClientTest {
public static void main(String[] args) throws Exception{
//start get
//1、創建客戶端
CloseableHttpClient httpClient = HttpClients.createDefault();
//2、創建Http的請求方式get,post etc... 需要傳入請求地址
HttpGet httpGet = new HttpGet("http://localhost:8888/study/test/query?name=liban");
//3、httpClient執行請求,並獲取返回結果
CloseableHttpResponse response = httpClient.execute(httpGet);
//4、輸出獲取到的結果
//從response中取出返回的entity
HttpEntity entity = response.getEntity();
//輸出
System.out.println("get返回狀態行:"+response.getStatusLine());
System.out.println("get返回結果:"+EntityUtils.toString(entity));
//5、關閉連接 也可以直接調用close()方法
HttpClientUtils.closeQuietly(response);
//end get
//start post
//1、創建post請求
HttpPost httpPost = new HttpPost("http://localhost:8888/study/test/query");
//2、設置請求參數HttpEntity
//UrlEncodedFormEntity的方式設置請求參數,這是假的post請求,參數是拼接再url中的如:?name=myName
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("name","myName"));
UrlEncodedFormEntity HttpEntity = new UrlEncodedFormEntity(pairs);
httpPost.setEntity(HttpEntity);
//3、發送請求
CloseableHttpResponse httpPostResponseUrl = httpClient.execute(httpPost);
//4、獲取返回結果
String postResultUrl = EntityUtils.toString(httpPostResponseUrl.getEntity());
//5、輸出結果
System.out.println("UrlEncodedFormEntity返回狀態行:"+httpPostResponseUrl.getStatusLine());
System.out.println("UrlEncodedFormEntity返回結果:"+postResultUrl);
//StringEntity的方式,這種方式比較自由,可以傳輸自定義的數據格式,只需要後臺能處理即可
//這種方式是真正的post請求,請求參數是防止body中,後臺通過流的方式獲取數據
//JSONObject使用阿里巴巴的fastjson
JSONObject jsonObject = new JSONObject();
jsonObject.put("name","StringEntity");
//創建StringEntity,並設置ContentType
StringEntity stringEntity = new StringEntity(jsonObject.toString(),ContentType.APPLICATION_JSON);
//stringEntity加入http中
httpPost.setEntity(stringEntity);
//3、發送http請求
CloseableHttpResponse httpPostResponse = httpClient.execute(httpPost);
//4、獲取返回結果
String postResult = EntityUtils.toString(httpPostResponse.getEntity());
//5、輸出結果
System.out.println("StringEntity返回狀態行:"+httpPostResponse.getStatusLine());
System.out.println("StringEntity返回結果:"+postResult);
//6、關閉
httpPostResponse.close();
//end post
//關閉client
HttpClientUtils.closeQuietly(httpClient);
}
}
輸出結果如下:
get返回狀態行:HTTP/1.1 200 OK
get返回結果:Welcome liban!,start doing Service ......ing
UrlEncodedFormEntity返回狀態行:HTTP/1.1 200 OK
UrlEncodedFormEntity返回結果:Welcome myName!,start doing Service ......ing
StringEntity返回狀態行:HTTP/1.1 200 OK
StringEntity返回結果:Welcome StringEntity!,start doing Service ......ing