|
Two days before the job, project leader gave me a remote interface to let me test it, because it is the http protocol, so I first thought of using httpClient tools to test, a search online, find a lot of sample code, casual copy a demo of a simple modification, the results of how tests are connection timeout, try a lot so that is not a demo, because we finally found a company to access the Internet through a proxy, so to perform this test you need to configure the agent .
The following is my test program
Use HttpClient remote interface testing
Jar kits:
package com.lym.test;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import com.google.gson.JsonObject;
public class HttpClientTest {
public static void main (String args []) throws Exception {
// Create HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create ();
// HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build ();
// Followed by requests for a destination address, port number, protocol type
HttpHost target = new HttpHost ( "61.144.244.6:8888/sztmerchant/merchant/addIsztMerchant.htm", 8888, "http");
// Followed by the proxy address, proxy port number, protocol type
HttpHost proxy = new HttpHost ( "proxy3.bj.petrochina", 8080, "http");
. RequestConfig config = RequestConfig.custom () setProxy (proxy) .build ();
// Address request
HttpPost httpPost = new HttpPost ( "http://61.144.244.6:8888/sztmerchant/merchant/addIsztMerchant.htm");
// Set header
httpPost.addHeader ( "Content-type", "application / json; charset = utf-8");
httpPost.setHeader ( "Accept", "application / json");
httpPost.setConfig (config);
// Create a string parameter json
JsonObject jsonObj = new JsonObject ();
jsonObj.addProperty ( "merchantNo", "33300911238");
jsonObj.addProperty ( "merchantName", "electricity supplier production test operations 1238");
String jsonStr = jsonObj.toString ();
System.out.println ( "parameters:" + jsonStr);
StringEntity entity;
try {
entity = new StringEntity (jsonStr, "UTF-8");
httpPost.setEntity (entity);
CloseableHttpResponse response = closeableHttpClient.execute (target, httpPost);
// GetEntity ()
HttpEntity httpEntity = response.getEntity ();
if (httpEntity! = null) {
// Print the content of the response
System.out.println ( "result:" + EntityUtils.toString (httpEntity, "UTF-8"));
} Else {
System.out.println ( "no response content");
}
// Release resources
if (closeableHttpClient! = null) {
closeableHttpClient.close ();
}
} Catch (Exception e) {
e.printStackTrace ();
}
}
} |
|
|
|