http-post call interface simple code

A simple and convenient httpget call interface, and the interface returns data
1, into a corresponding jar package;

2, as follows:

HttpPost post = null;
try {
HttpClient httpClient = new DefaultHttpClient();
// 设置超时时间
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 20000);

post = new HttpPost(url);
// 构造消息头
// post.setHeader("Content-type", contenttype);
post.setHeader("rxToken", rxToken);
post.setHeader("source", source);
Map map = new HashMap<>();
// map.put("bizType", "1");
// map.put("pageNo", 1);
// map.put("pageSize", 1000);
// map.put("orgId", 1);
// List list=new ArrayList<>();
// list.add(0);
// list.add(1);
Map.put // ( "the memberTypes", List);
// map.put ( "isLosts", List);
the JSONObject the JSONObject JSON new new = (Map);
// build message entity
StringEntity entity = new StringEntity (json.toString ( ), Charset.forName ( "UTF-. 8"));
entity.setContentEncoding ( "UTF-. 8");
a data transmission Json format // request
entity.setContentType ( "file application / JSON");
post.setEntity (Entity) ;
the HttpResponse Response = httpClient.execute (POST);
// return code inspection
int = statusCode response.getStatusLine () getStatusCode ();.
IF (! = statusCode HttpStatus.SC_OK) {
System.out.println ( "error Interface returned = == "+ statusCode);
the HttpEntity Entity1 response.getEntity = (); // get the response entity
long aaa = entity1.getContentLength (); // Get a data size
if (aaa == -1) {// if it is -1, the reset date_size
}
Content = EntityUtils.toString (Entity1); // parse the response
System.out.println ( "interface has returned ===" + content) ;
} {the else
the HttpEntity Entity1 response.getEntity = (); // get the response entity
long aaa = entity1.getContentLength (); // Get the size of the corresponding data
if (aaa == -1) {// If -1, reset date_size
}
Content = EntityUtils.toString (Entity1); // parse the response
System.out.println ( "interface has returned ===" Content +);
}
} the catch (Exception E) {
e.printStackTrace ();
} the finally {
IF (POST = null!) {
the try {
post.releaseConnection ();
the Thread.sleep (500);
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
}
}

 3, return data to determine whether the interface according to your needs.

Guess you like

Origin www.cnblogs.com/xescbky/p/11015124.html