httpclient作用
在java代碼中,發(fā)送Http請求舶胀。通常用來實(shí)現(xiàn)遠(yuǎn)程接口調(diào)用赊堪。
HttpClient測試
在工程中添加httpclient的pom依賴。
<dependency>
????<groupId>org.apache.httpcomponents</groupId>
????<artifactId>httpclient</artifactId>
</dependency>
1.執(zhí)行GET請求
public static void doGet(){
? // 1.創(chuàng)建Httpclient對象
???CloseableHttpClient httpclient = HttpClients.createDefault();
???// 2.創(chuàng)建http GET請求
???HttpGet httpGet = new HttpGet("http://www.oschina.net/");
???CloseableHttpResponse response = null;
???try {
??????// 3.執(zhí)行請求
??????response = httpclient.execute(httpGet);
??????System.out.println(response.getStatusLine());
?????// 4.判斷返回狀態(tài)是否為200
?????if (response.getStatusLine().getStatusCode() == 200) {
???????String content = EntityUtils.toString(response.getEntity(), "UTF-8");
???????System.out.println("內(nèi)容長度:" + content.length());
??????}
???}catch(Exception e){
?????e.printStackTrace();
??} finally {
??????if (response != null) {
?????????try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
???}
???try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2.執(zhí)行GET帶參數(shù)
public static void doGetParam(){
??// 1.創(chuàng)建Httpclient對象
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
????// 2.定義請求的參數(shù)
???URI uri = newURIBuilder("http://www.baidu.com/s").setParameter("wd", "數(shù)據(jù)庫").build();
????// 3.創(chuàng)建http GET請求
????HttpGet httpGet = new HttpGet(uri);
?// 4.執(zhí)行請求
???response = httpclient.execute(httpGet);
????// 5.判斷返回狀態(tài)是否為200
???if (response.getStatusLine().getStatusCode() == 200) {
???????String content = EntityUtils.toString(response.getEntity(), "UTF-8");
???????System.out.println(content);
???????}
????}catch(Exception e){
???????e.printStackTrace();
????}finally {
??????if (response != null) {
?????????try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
????}
????try {
httpclient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
??}
}
?
3.執(zhí)行post請求
public static void doPost(){
?// 1.創(chuàng)建Httpclient對象
??CloseableHttpClient httpclient =?HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
?// 2.創(chuàng)建http POST請求
???HttpPost httpPost = new HttpPost("http://www.oschina.net/");
???CloseableHttpResponse response = null;
???try {
??????// 3.執(zhí)行請求
???????response = httpclient.execute(httpPost);
???????System.out.println(response.getStatusLine());
??????// 判斷返回狀態(tài)是否為200
???????if (response.getStatusLine().getStatusCode() == 200) {
??????????String content = EntityUtils.toString(response.getEntity(), "UTF-8");
??????????System.out.println(content);
???????}
?????}catch(Exception e){
?????????e.printStackTrace();
????}finally {
???????if (response != null) {
??????????try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
????}
????try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
???}
?}
?
?
4.執(zhí)行post帶參數(shù)
public static void doPostParam() throws Exception{
??// 1.創(chuàng)建Httpclient對象
??CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
???// 2.創(chuàng)建http POST請求
HttpPost httpPost = new HttpPost("http://www.oschina.net/search"); ??
//3.給post 設(shè)置請求頭(重中之重,否則被調(diào)用服務(wù)接收不到參數(shù),且需要用jsonObject接收)??
httpPost.setHeader("Content-Type","application/json");
??//4.?給post設(shè)置參數(shù)
JSONObject param = new JSONObject();
StringEntity entity = new String(param.toString());
//entity 設(shè)置字符編碼,防止亂碼
Entity.setContentEncoding("UTF-8");
??// 將請求實(shí)體參數(shù)設(shè)置到httpPost對象中
???httpPost.setEntity(entity);
???CloseableHttpResponse response = null;
???try {
??????// 執(zhí)行請求
??????response = httpclient.execute(httpPost);
??????System.out.println(response.getStatusLine());
??????// 判斷返回狀態(tài)是否為200
??????if (response.getStatusLine().getStatusCode() == 200) {
?????String content = EntityUtils.toString(response.getEntity(), "UTF-8");
?????System.out.println(content);
????}
??} finally {
?????if (response != null) {
???????????response.close();
????}
??????httpclient.close();
}
}
?
?