長話短說,廢話不說

一、第一種方式,通過HttpClient方式,代碼如下:
public static String httpGet(String url, String charset)
throws HttpException, IOException {
String json = null;
HttpGet httpGet = new HttpGet();
// 設(shè)置參數(shù)
try {
httpGet.setURI(new URI(url));
} catch (URISyntaxException e) {
throw new HttpException("請求url格式錯誤。"+e.getMessage());
}
// 發(fā)送請求
HttpResponse httpResponse = client.execute(httpGet);
// 獲取返回的數(shù)據(jù)
HttpEntity entity = httpResponse.getEntity();
byte[] body = EntityUtils.toByteArray(entity);
StatusLine sL = httpResponse.getStatusLine();
int statusCode = sL.getStatusCode();
if (statusCode == 200) {
json = new String(body, charset);
entity.consumeContent();
} else {
throw new HttpException("statusCode="+statusCode);
}
return json;
}
分享題目:java發(fā)送httpget請求的兩種方式-創(chuàng)新互聯(lián)
鏈接地址:http://www.chinadenli.net/article4/dehgoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、微信公眾號、網(wǎng)站策劃、品牌網(wǎng)站制作、網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)