遠(yuǎn)程接口調(diào)用方式HttpClient

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();

}

}

?






?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容