使用HttpClient進行遠程介面測試,httpclient介面

來源:互聯網
上載者:User

使用HttpClient進行遠程介面測試,httpclient介面

前兩天在工作中,項目組長給我了一個遠程介面讓我給測一下,因為是http協議,所以我首先想到了用httpClient工具類來測試,網上一查,找到了好多範例程式碼,隨便複製了一個demo進行了簡單的修改,結果怎麼測試都是連線逾時,試了很多個demo也不好使,最後發現是因為我們公司訪問外網是通過代理,所以在進行測試的時候需要配置代理。一下是我的測試程式

用到的jar包:

 1 package com.lym.test; 2  3 import org.apache.http.HttpEntity; 4 import org.apache.http.HttpHost; 5 import org.apache.http.client.config.RequestConfig; 6 import org.apache.http.client.methods.CloseableHttpResponse; 7 import org.apache.http.client.methods.HttpPost; 8 import org.apache.http.entity.StringEntity; 9 import org.apache.http.impl.client.CloseableHttpClient;10 import org.apache.http.impl.client.HttpClientBuilder;11 import org.apache.http.util.EntityUtils;12 13 import com.google.gson.JsonObject;14 15 public class HttpClientTest {16     17     public static void main(String args[]) throws Exception {18         19         // 建立HttpClientBuilder20         HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();21         // HttpClient22         CloseableHttpClient closeableHttpClient = httpClientBuilder.build();23         // 依次是目標請求地址,連接埠號碼,協議類型24         HttpHost target = new HttpHost("61.144.244.6:8888/sztmerchant/merchant/addIsztMerchant.htm", 8888, "http");25         // 依次是Proxy 位址,代理連接埠號碼,協議類型26         HttpHost proxy = new HttpHost("proxy3.bj.petrochina", 8080, "http");27         RequestConfig config = RequestConfig.custom().setProxy(proxy).build();28 29         // 請求地址30         HttpPost httpPost = new HttpPost("http://61.144.244.6:8888/sztmerchant/merchant/addIsztMerchant.htm");31         // 設定頭資訊32         httpPost.addHeader("Content-type", "application/json; charset=utf-8");33         httpPost.setHeader("Accept", "application/json");34         httpPost.setConfig(config);35         36         // 建立參數json串37         JsonObject jsonObj = new JsonObject();38         jsonObj.addProperty("merchantNo", "33300911238");39         jsonObj.addProperty("merchantName", "電商運營生產測試1238");40         String jsonStr = jsonObj.toString();41         System.out.println("parameters: " + jsonStr);42         43         StringEntity entity;44         try {45             entity = new StringEntity(jsonStr, "UTF-8");46             httpPost.setEntity(entity);47             CloseableHttpResponse response = closeableHttpClient.execute(target, httpPost);48             // getEntity()49             HttpEntity httpEntity = response.getEntity();50             if (httpEntity != null) {51                 // 列印響應內容52                 System.out.println("result: " + EntityUtils.toString(httpEntity, "UTF-8"));53             }else {54                 System.out.println("無響應內容");55             }56             // 釋放資源57             if(closeableHttpClient != null) {58                 closeableHttpClient.close();                59             }60         } catch (Exception e) {61             e.printStackTrace();62         }63     }64     65 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.