httpclient 小例子编写
生活随笔
收集整理的這篇文章主要介紹了
httpclient 小例子编写
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
?
最近這兩天幫忙調(diào)試接口,用到了httpclient ,自己參照網(wǎng)上方法編寫個(gè)小例子 ,方便以后查看
-----
?
/*** 遠(yuǎn)程訪問調(diào)用方法工具類* * @author yangy* */ public class RemoteRequestUtils {public static void main(String[] args) throws HttpException, IOException{getRequest(null);}/*** 遠(yuǎn)程get請(qǐng)求 方法直接打印返回結(jié)果* * @param url* @param parmete* @throws IOException* @throws HttpException* @throws Exception*/public static void getRequest(String url) throws HttpException, IOException{// 創(chuàng)建請(qǐng)求對(duì)象HttpClient client = new HttpClient();client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");//設(shè)置超時(shí)時(shí)間client.getParams().setSoTimeout(2000);// 創(chuàng)建遠(yuǎn)程訪問GetMethod method = new GetMethod(url);// 設(shè)置成了默認(rèn)的恢復(fù)策略,在發(fā)生異常時(shí)候?qū)⒆詣?dòng)重試3次,在這里你也可以設(shè)置成自定義的恢復(fù)策略method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());// 執(zhí)行g(shù)etMethodint statusCode = client.executeMethod(method);if (statusCode != HttpStatus.SC_OK){System.err.println("Method failed: " + method.getStatusLine());}//獲取數(shù)據(jù)String responseBody = readInputStream(method.getResponseBodyAsStream());System.out.println(" Remoter request success :" + responseBody);// 釋放連接method.releaseConnection();}/*** 遠(yuǎn)程post請(qǐng)求 方法直接打印返回結(jié)果* * @param url* @param parmete* @throws IOException* @throws HttpException*/public static void postRequest(String url, NameValuePair[] data)throws HttpException, IOException{// 創(chuàng)建請(qǐng)求對(duì)象HttpClient httpClient = new HttpClient();// 設(shè)置超時(shí)時(shí)間httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(20000);// 創(chuàng)建POST對(duì)象UTF8PostMethod postMethod = new UTF8PostMethod(url);// 設(shè)置成了默認(rèn)的恢復(fù)策略,在發(fā)生異常時(shí)候?qū)⒆詣?dòng)重試3次,在這里你也可以設(shè)置成自定義的恢復(fù)策略postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());// 將表單的值放入postMethod中postMethod.setRequestBody(data);// 執(zhí)行postMethodint statusCode = httpClient.executeMethod(postMethod);// HttpClient對(duì)于要求接受后繼服務(wù)的請(qǐng)求,象POST和PUT等不能自動(dòng)處理轉(zhuǎn)發(fā)System.out.println(statusCode);String location = "";// 返回返回結(jié)果if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY){// 從頭中取出轉(zhuǎn)向的地址Header locationHeader = postMethod.getResponseHeader("location");if (locationHeader != null){location = locationHeader.getValue();System.out.println("The page was redirected to:" + location);} else{System.err.println("Location field value is null.");}} else if (statusCode == HttpStatus.SC_OK) // 返回為連接成功{// 此處修改為返回為輸入流 避免大數(shù)量時(shí)出錯(cuò) 消耗內(nèi)存String responseBody = readInputStream(postMethod.getResponseBodyAsStream());System.out.println(" Remoter request success :" + responseBody);}// 釋放連接postMethod.releaseConnection();}/*** 轉(zhuǎn)換Inputstram為字符串* * @param responseBody* @return * @throws IOException*/private static String readInputStream(InputStream responseBody)throws IOException{//創(chuàng)建字符串?dāng)?shù)讀取緩存BufferedReader buffre = new BufferedReader(new InputStreamReader(responseBody,"utf-8")); //使用utf-8 避免出現(xiàn)亂碼//創(chuàng)建緩存字符串StringBuffer resBuffer = new StringBuffer(10000);String resTemp = "";while ((resTemp = buffre.readLine()) != null){resBuffer.append(resTemp).append("\n"); //讀取字符流中數(shù)據(jù) 目前測(cè)試可能存在換行添加}return resBuffer.toString();}}目前代碼能夠執(zhí)行成功, 蛋痛的是 調(diào)用對(duì)方接口 每次一調(diào)用他們的系統(tǒng)就掛掉...
?
轉(zhuǎn)載于:https://my.oschina.net/luckyi/blog/69188
總結(jié)
以上是生活随笔為你收集整理的httpclient 小例子编写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 命令之 --[chattr]
- 下一篇: 3D Button Visual Edi