課程
/后端開(kāi)發(fā)
/Java
/Java微信公眾號(hào)開(kāi)發(fā)進(jìn)階
defaultHttpClient 警告過(guò)時(shí),有什么可以替代的嗎
2016-06-22
源自:Java微信公眾號(hào)開(kāi)發(fā)進(jìn)階 2-2
正在回答
DefaultHttpClient 用 CloseableHttpClient
HttpResponse 用 CloseableHttpResponse
官方新api的樣例
Get方法:
????CloseableHttpClient?httpclient?=?HttpClients.createDefault(); ????HttpGet?httpGet?=?new?HttpGet("http://targethost/homepage"); ????CloseableHttpResponse?response1?=?httpclient.execute(httpGet); ????//?The?underlying?HTTP?connection?is?still?held?by?the?response?object ????//?to?allow?the?response?content?to?be?streamed?directly?from?the?network?socket. ????//?In?order?to?ensure?correct?deallocation?of?system?resources ????//?the?user?MUST?either?fully?consume?the?response?content??or?abort?request ????//?execution?by?calling?CloseableHttpResponse#close(). ????try?{ ????????System.out.println(response1.getStatusLine()); ????????HttpEntity?entity1?=?response1.getEntity(); ????????//?do?something?useful?with?the?response?body ????????//?and?ensure?it?is?fully?consumed ????????EntityUtils.consume(entity1); ????}?finally?{ ????????response1.close(); ????}
Post方法:
??????HttpPost?httpPost?=?new?HttpPost("http://targethost/login"); ????//拼接參數(shù) ????List?<NameValuePair>?nvps?=?new?ArrayList?<NameValuePair>(); ????nvps.add(new?BasicNameValuePair("username",?"vip")); ????nvps.add(new?BasicNameValuePair("password",?"secret")); ????httpPost.setEntity(new?UrlEncodedFormEntity(nvps)); ????CloseableHttpResponse?response2?=?httpclient.execute(httpPost); ????try?{ ????????System.out.println(response2.getStatusLine()); ????????HttpEntity?entity2?=?response2.getEntity(); ????????//?do?something?useful?with?the?response?body ????????//?and?ensure?it?is?fully?consumed ????????//消耗掉response ????????EntityUtils.consume(entity2); ????}?finally?{ ????????response2.close(); ????}
導(dǎo)入httpclient 4.2.5
導(dǎo)入httpcore 4.2.4
我跟你遇到過(guò)同樣的問(wèn)題,親測(cè)有效
qq_酷愛(ài)達(dá)人_0
漂泊流浪的懶漢 回復(fù) qq_酷愛(ài)達(dá)人_0
CloseableHttpClient httpClient = HttpClients.createDefault();
舉報(bào)
Java微信公眾號(hào)開(kāi)發(fā)的進(jìn)階課程,在入門的基礎(chǔ)上更加深入
1 回答執(zhí)行到 這一步的時(shí)候 ,DefaultHttpClient httpClient = new DefaultHttpClient(); 報(bào)錯(cuò)
1 回答DefaultHttpClient 和 HttpGet是什么
4 回答defaulthttpclient定義的變量有什么用的呀?
2 回答DefaultHttpClient現(xiàn)在不能用了是否用CloseableHttpClient 代替?
1 回答請(qǐng)問(wèn)這個(gè)怎么處理
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2018-05-18
DefaultHttpClient 用 CloseableHttpClient
HttpResponse 用 CloseableHttpResponse
官方新api的樣例
Get方法:
Post方法:
2016-07-31
導(dǎo)入httpclient 4.2.5
導(dǎo)入httpcore 4.2.4
我跟你遇到過(guò)同樣的問(wèn)題,親測(cè)有效
2016-07-21
CloseableHttpClient httpClient = HttpClients.createDefault();