httpclient之入門使用

來源:互聯網
上載者:User

標籤:pre   art   部分   class   請求   建立   UI   block   apache   

1.簡介

          HTTP 協議可能是現在 Internet 上使用得最多、最重要的協議了,越來越多的 Java 應用程式需要直接通過 HTTP 協議來訪問網路資源。雖然在 JDK 的 java net包中已經提供了訪問 HTTP 協議的準系統,但是對於大部分應用程式來說,JDK 庫本身提供的功能還不夠豐富和靈活。HttpClient 是 Apache Jakarta Common 下的子項目,用來提供高效的、最新的、功能豐富的支援 HTTP 協議的用戶端編程工具包,並且它支援 HTTP 協議最新的版本和建議。

2.使用情境

           HttpClient可以靈活地訪問網路資源,他不僅可以靈活地在用戶端訪問服務端的網路資源。在服務端,也可以通過HttpClient完成跨層次訪問。

3.使用

         下面可以通過HttpClientUtil清晰的掌握HttpClient的基本使用方法:(GET,POST和JSON的提交)

  

  1 public class HttpClientUtil {  2   3     public static String doGet(String url, Map<String, String> param) {  4   5         // 建立Httpclient對象  6         CloseableHttpClient httpclient = HttpClients.createDefault();  7   8         String resultString = "";  9         CloseableHttpResponse response = null; 10         try { 11             // 建立uri 12             URIBuilder builder = new URIBuilder(url); 13             if (param != null) { 14                 //拼接請求參數 15                 for (String key : param.keySet()) { 16                     builder.addParameter(key, param.get(key)); 17                 } 18             } 19             URI uri = builder.build(); 20  21             // 建立http GET請求 22             HttpGet httpGet = new HttpGet(uri); 23  24             // 執行請求 25             response = httpclient.execute(httpGet); 26             // 判斷返回狀態是否為200 27             if (response.getStatusLine().getStatusCode() == 200) { 28                 resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); 29             } 30         } catch (Exception e) { 31             e.printStackTrace(); 32         } finally { 33             try { 34                 if (response != null) { 35                     response.close(); 36                 } 37                 httpclient.close(); 38             } catch (IOException e) { 39                 e.printStackTrace(); 40             } 41         } 42         return resultString; 43     } 44  45     public static String doGet(String url) { 46         return doGet(url, null); 47     } 48  49     public static String doPost(String url, Map<String, String> param) { 50         // 建立Httpclient對象 51         CloseableHttpClient httpClient = HttpClients.createDefault(); 52         //建立HttpResponse響應 53         CloseableHttpResponse response = null; 54         String resultString = ""; 55         try { 56             // 建立Http Post請求 57             HttpPost httpPost = new HttpPost(url); 58             // 建立參數列表 59             if (param != null) { 60                 //拼接表單參數 61                 List<NameValuePair> paramList = new ArrayList<>(); 62                 for (String key : param.keySet()) { 63                     paramList.add(new BasicNameValuePair(key, param.get(key))); 64                 } 65                 // 類比表單 66                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); 67                 httpPost.setEntity(entity); 68             } 69             // 執行http請求 70             response = httpClient.execute(httpPost); 71             resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 72         } catch (Exception e) { 73             e.printStackTrace(); 74         } finally { 75             try { 76                 response.close(); 77             } catch (IOException e) { 78                 // TODO Auto-generated catch block 79                 e.printStackTrace(); 80             } 81         } 82  83         return resultString; 84     } 85  86     public static String doPost(String url) { 87         return doPost(url, null); 88     } 89      90     public static String doPostJson(String url, String json) { 91         // 建立Httpclient對象 92         CloseableHttpClient httpClient = HttpClients.createDefault(); 93         CloseableHttpResponse response = null; 94         String resultString = ""; 95         try { 96             // 建立Http Post請求 97             HttpPost httpPost = new HttpPost(url); 98             // 建立請求內容 99             StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);100             //設定Post請求101             httpPost.setEntity(entity);102             // 執行http請求103             response = httpClient.execute(httpPost);104             resultString = EntityUtils.toString(response.getEntity(), "utf-8");105         } catch (Exception e) {106             e.printStackTrace();107         } finally {108             try {109                 response.close();110             } catch (IOException e) {111                 // TODO Auto-generated catch block112                 e.printStackTrace();113             }114         }115 116         return resultString;117     }118 }

 

httpclient之入門使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.