遠(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)系作者
  • 序言:七十年代末履澳,一起剝皮案震驚了整個(gè)濱河市柱徙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌奇昙,老刑警劉巖护侮,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異储耐,居然都是意外死亡羊初,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來长赞,“玉大人晦攒,你說我怎么就攤上這事〉枚撸” “怎么了脯颜?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長贩据。 經(jīng)常有香客問我栋操,道長,這世上最難降的妖魔是什么饱亮? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任矾芙,我火速辦了婚禮苟蹈,結(jié)果婚禮上寝姿,老公的妹妹穿的比我還像新娘逮走。我一直安慰自己宦焦,他們只是感情好殷绍,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布毙玻。 她就那樣靜靜地躺著谬以,像睡著了一般门扇。 火紅的嫁衣襯著肌膚如雪斗锭。 梳的紋絲不亂的頭發(fā)上哈街,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天,我揣著相機(jī)與錄音拒迅,去河邊找鬼骚秦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛璧微,可吹牛的內(nèi)容都是我干的作箍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼前硫,長吁一口氣:“原來是場噩夢啊……” “哼胞得!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起屹电,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤阶剑,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后危号,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體牧愁,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年外莲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了猪半。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片兔朦。...
    茶點(diǎn)故事閱讀 40,498評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖磨确,靈堂內(nèi)的尸體忽然破棺而出沽甥,到底是詐尸還是另有隱情,我是刑警寧澤乏奥,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布摆舟,位于F島的核電站,受9級特大地震影響邓了,放射性物質(zhì)發(fā)生泄漏恨诱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一驶悟、第九天 我趴在偏房一處隱蔽的房頂上張望胡野。 院中可真熱鬧材失,春花似錦痕鳍、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至旨别,卻和暖如春诗赌,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背秸弛。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工铭若, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人递览。 一個(gè)月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓叼屠,卻偏偏與公主長得像,于是被迫代替她去往敵國和親绞铃。 傳聞我的和親對象是個(gè)殘疾皇子镜雨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容