HttpClient示例及资源释放
点关注不迷路,欢迎再访!
精简博客内容,尽量已行业术语来分享。
努力做到对每一位认可自己的读者负责。
帮助别人的同时更是丰富自己的良机。
一.PostMethod
public class TestHttpclient extends TestCase {
public static void main(String args[]) {
String url = "http://x.x.x.x:x/project/hrUserInvalid.do";
String userName ="xxxxxx";
HttpClient client = new HttpClient();
PostMethod method = null;
try {
method = new PostMethod(url);
method.addParameter("userName", userName);
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(method);
System.out.println("请求状态码:"+status);
String result = method.getResponseBodyAsString();
System.out.println("结果:"+result);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (method != null) {
method.releaseConnection();
//大量请求访问时就必然会造成链接被占满,请求等待的情况
//HttpClient在method.releaseConnection()后并没有把链接关闭,这个方法只是将链接返回给connection manager。如果使用HttpClient client = new HttpClient()实例化一个HttpClient connection manager默认实现是使用SimpleHttpConnectionManager。
//SimpleHttpConnectionManager有个构造函数如下
//public SimpleHttpConnectionManager(boolean alwaysClose) {
//super();
//this.alwaysClose = alwaysClose;
//}
//看远程注释我们就可以看到如果alwaysClose设为true在链接释放之后connection manager 就会关闭链。
//因此alwaysClose默认是false,connection是不会被主动关闭的,因此我们就有了一个客户端关闭链接的方法。
//客户端主动释放链接
//1.HttpClient client = new HttpClient(new HttpClientParams(),new SimpleHttpConnectionManager(true));
//2.client.getHttpConnectionManager().closeIdleConnections(0);
//此方法源码代码如下:
//public void closeIdleConnections(long idleTimeout) {
//long maxIdleTime = System.currentTimeMillis() - idleTimeout;
//if (idleStartTime <= maxIdleTime) {
//httpConnection.close();
//}
//}
}
}
}
}
二.GetMethod
public void httpGetMethod() {
String url ="http://x.x.x.x:x/project/shorten.json?source=327176057";
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
try {
int status = client.executeMethod(method);
System.out.println("请求状态码 " + status);
String result = method.getResponseBodyAsString();
System.out.println("结果:"+result);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (method != null) {
method.releaseConnection();
}
}
}
三.HttpPost
public void httpPostMethod(){
String url = "http://xxxx/PSIGW/RESTListeningConnector/PSFT_HR/UM_EMPLOYEE.v1/";
String params = "{\"client_id\":\"UM_SYSTEM\",\"sign\":\"5F3131637EFA612C23507739293A2423\",\"hr_status\":\"A\"}";
//httpclient反复创建开销
//httpclient是一个线程安全的类,没有必要由每个线程在每次使用时创建,全局保留一个即可。
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=GBK");
StringEntity stringEntity = new StringEntity(params);
stringEntity.setContentType("text/json");
//传递参数
httpPost.setEntity(stringEntity);
//通过client来执行请求,获取响应结果
CloseableHttpResponse response = httpclient.execute(httpPost);
try {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,"GBK");
System.out.println("结果:"+result);
} catch (ParseException e) {
e.printStackTrace();
} finally {
response.close();
}
//httpclient.close();
}
}