Use HttpClient for remote interface test and httpclient Interface

Source: Internet
Author: User

Use HttpClient for remote interface test and httpclient Interface

During my work two days ago, the project leader gave me a remote interface and asked me to test it. Because it was an http protocol, I first thought of using the httpClient tool class for testing and checking it online, after finding a lot of sample code, I copied a demo and made some simple modifications. The connection times out in the test results, and it is hard to make a lot of demos, finally, we found that our company accesses the Internet through a proxy, so we need to configure the proxy during the test. Here is my test program.

Jar package used:

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. cl Ient. 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 // create HttpClientBuilder20 HttpClientBuilder httpClientBuilder = HttpClientBuilder. create (); 21 // HttpClient22 CloseableHttpClient closeableHttpClient = httpClientBuilder. build (); 23 // target requests in turn Address, port number, protocol type 24 HttpHost target = new HttpHost ("61.144.244.6: 8888/sztmerchant/merchant/addIsztMerchant.htm", 8888, "http"); 25 // proxy address, proxy port number, protocol type 26 HttpHost proxy = new HttpHost ("proxy3.bj. petrochina ", 8080," http "); 27 RequestConfig config = RequestConfig. custom (). setProxy (proxy ). build (); 28 29 // request address 30 HttpPost httpPost = new HttpPost ("http: // 61.144.244.6: 8888/sztmerchant/merchant/addIsztMerchant. Htm "); 31 // sets the header information 32 httpPost. addHeader ("Content-type", "application/json; charset = UTF-8"); 33 httpPost. setHeader ("Accept", "application/json"); 34 httpPost. setConfig (config); 35 36 // create parameter json string 37 JsonObject jsonObj = new JsonObject (); 38 jsonObj. addProperty ("merchantNo", "33300911238"); 39 jsonObj. addProperty ("merchantName", "e-commerce operation and production test 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.exe cute (target, httpPost); 48 // getEntity () 49 HttpEntity httpEntity = response. getEntity (); 50 if (httpEntity! = Null) {51 // print the response content 52 System. out. println ("result:" + EntityUtils. toString (httpEntity, "UTF-8"); 53} else {54 System. out. println ("no response content"); 55} 56 // release resource 57 if (closeableHttpClient! = Null) {58 closeableHttpClient. close (); 59} 60} catch (Exception e) {61 e. printStackTrace (); 62} 63} 64 65}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.