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