您的位置:首页 > 理论基础 > 计算机网络

简单封装的httpclient4请求类

2015-08-09 22:13 561 查看
项目中经常会用到跨项目之间的请求,这时候我们经常采用的httpclient,由于大多数情况下并用不到一些非常复杂的功能,所以这里我将一些基本的请求封装了一下,以方便日常开发中使用。

以下类实现了如果重新new 一个Class的情况下,多次请求使用的是一个session。

package util.http;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import util.common.MapUtil;

public class SimpleHttpClient {

private HttpClientBuilder httpclientBuilder;
private CloseableHttpClient httpclient;
private HttpContext context;
private Header[] responseHeaders;

public SimpleHttpClient() {
//httpclient = HttpClients.createDefault();
httpclientBuilder = HttpClients.custom();
httpclient = httpclientBuilder.build();
context = new BasicHttpContext();
}

@SuppressWarnings("unchecked")
private List<BasicNameValuePair> toNameValuePairList(Map<String, String> m) {
List<BasicNameValuePair> nameValuePairList = new ArrayList<BasicNameValuePair>();
List<String> keylist = MapUtil.keyList(m);
for (String key : keylist) {
nameValuePairList.add(new BasicNameValuePair(key, m.get(key)));
}
return nameValuePairList;
}

public String get(String url) throws ClientProtocolException, IOException {
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = httpclient.execute(httpget, context);
responseHeaders = response.getAllHeaders();
String result = EntityUtils.toString(response.getEntity());
response.close();
return result;
}

public String post(String url) throws ClientProtocolException, IOException {
HttpPost httppost = new HttpPost(url);
CloseableHttpResponse response = httpclient.execute(httppost, context);
responseHeaders = response.getAllHeaders();
String result = EntityUtils.toString(response.getEntity());
response.close();
return result;
}

public String post(String url, Header[] headers) throws ClientProtocolException, IOException {
HttpPost httppost = new HttpPost(url);
for (Header header : headers) {
httppost.setHeader(header);
}
CloseableHttpResponse response = httpclient.execute(httppost, context);
responseHeaders = response.getAllHeaders();
String result = EntityUtils.toString(response.getEntity());
response.close();
return result;
}

public String post(String url, Map<String, String> params) throws ClientProtocolException, IOException {
HttpPost httppost = new HttpPost(url);
List<BasicNameValuePair> nameValuePairList = toNameValuePairList(params);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairList,"UTF-8"));
CloseableHttpResponse response = httpclient.execute(httppost, context);
responseHeaders = response.getAllHeaders();
String result = EntityUtils.toString(response.getEntity());
response.close();
return result;
}

public String post(String url, List<Header> headers, Map<String, String> params) throws ClientProtocolException, IOException {
httpclient = httpclientBuilder.disableCookieManagement().setDefaultHeaders(headers).build();
HttpPost httppost = new HttpPost(url);
List<BasicNameValuePair> nameValuePairList = toNameValuePairList(params);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairList,"UTF-8"));
CloseableHttpResponse response = httpclient.execute(httppost, context);
responseHeaders = response.getAllHeaders();
String result = EntityUtils.toString(response.getEntity());
response.close();
return result;
}

public InputStream execute(String url) throws ClientProtocolException, IOException {
HttpPost httppost = new HttpPost(url);
CloseableHttpResponse response = httpclient.execute(httppost, context);
responseHeaders = response.getAllHeaders();
InputStream is = response.getEntity().getContent();
return is;
}

public List<Header> getResponseHeader(String name) {
List<Header> hs = new ArrayList<Header>();
for (Header header : responseHeaders) {
String n = header.getName();
//String v = header.getValue();
if (n.equals(name)) {
hs.add(header);
}
}
return hs;
}

public void close() throws IOException {
this.httpclient.close();
}
public CloseableHttpClient getHttpclient() {
return httpclient;
}
public HttpContext getContext() {
return context;
}
public Header[] getResponseHeaders() {
return responseHeaders;
}
public HttpClientBuilder getHttpclientBuilder() {
return httpclientBuilder;
}

//示例
@SuppressWarnings("unused")
private static void main(String[] args) throws ClientProtocolException, IOException {
SimpleHttpClient hc = new SimpleHttpClient();
String url = "http://xxxxxxxxxxxxxxxxxx";
Map<String, String> params = new HashMap<String, String>();
params.put("phone", "1380003333");
params.put("captcha", "abcde");

String result = hc.post(url, params);
System.out.println(result);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: