On the basis of HttpClient made a further step of encapsulation
- Support json, xml format and other String data requests.
- Support resource file upload, multi file upload, resource file download
- Support modification and acquisition of Http header
- Support Https and Http request
surroundings
The main jar package
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
method
- sendData (): basic data request, support POST GET DELETE 3 request method
- sendResource (): send resource files, support multi-file upload
- getResource (): resource file download
Return value (sendData (), sendResource ())
Return value type |
response |
result |
Map |
HTTPEntity |
String |
The return value (except getResource ()) contains two parameters for the map type
- response HTTPEntity type
- result String type
getResoure()
Return value type |
type |
strram |
Map |
String |
InputStream |
Contains two parameters
- type: return the content-type of the resource
- InputStream resource stream
Class attribute
//请求方式
public static final String POST = "POST";
public static final String GET = "GET";
public static final String DELETE = "DELETE";
//普通string数据以及xml格式
private static String xmldata;
//javaBean 通过Gson转为json格式
private Object object;
//请求url
private String url;
//请求方式
private String method;
//请求头
private Map<String, String> header;
//发送文件路径
private String[] filepath;
//发送文件key值
private String[] filename;
Code
//HttpConnect.java
package org.apache.myfaces.blank;
import com.google.gson.Gson;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.nio.charset.Charset;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Forgot on 2017/1/12.
*/
public class HttpConnect {
public static final String POST = "POST";
public static final String GET = "GET";
public static final String DELETE = "DELETE";
private static String xmldata;
private Object object;
private String url;
private String method;
private Map<String, String> header;
private String[] filepath;
private String[] filename;
public static class Builder {
private String xmldata;
private Object object;
private String url;
private String method;
private Map<String, String> header;
private String[] filepath;
private String[] filename;
private Builder filename(String[] val){
filename= val;
return this;
}
private Builder filepath(String[] val){
filepath= val;
return this;
}
public Builder xmldata(String val) {
xmldata = val;
return this;
}
public Builder object(Object val) {
object = val;
return this;
}
public Builder url(String val) {
url = val;
return this;
}
public Builder method(String val) {
method = val;
return this;
}
public Builder header(Map<String, String> val) {
header = val;
return this;
}
public HttpConnect bulid() {
return new HttpConnect(this);
}
}
private HttpConnect(Builder builder) {
xmldata = builder.xmldata;
object = builder.object;
url = builder.url;
method = builder.method;
header = builder.header;
filepath = builder.filepath;
filename = builder.filename;
}
Gson gson = new Gson();
/**
* @return
* @throws IOException
*/
public Map<String,Object> sendData() throws IOException, StatementException {
Map<String,Object> map = new HashMap<>();
if(method == null || url ==null){
throw new StatementException("必要的参数缺失");
}
switch (method) {
case "POST": {
HttpClient httpClient = getHttpClient();
HttpPost httpPost = new HttpPost(url);
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
if (object != null && xmldata == null) {
StringEntity entity = new StringEntity(gson.toJson(object), "UTF-8");//设置StringEntity编码为utf-8
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8"));
map.put("response",response);
map.put("result",result);
return map;
} else if (xmldata != null && object == null) {
StringEntity entity = new StringEntity(xmldata, "UTF-8");//设置StringEntity编码为utf-8
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8"));
map.put("response",response);
map.put("result",result);
return map;
} else {
HttpResponse response = httpClient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8"));
map.put("response",response);
map.put("result",result);
return map;
}
}
case "GET": {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
HttpResponse response = httpClient.execute(httpGet);
String result = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8"));
map.put("response",response);
map.put("result",result);
return map;
}
case "DELETE": {
HttpClient httpClient = new DefaultHttpClient();
HttpDelete httpDelete = new HttpDelete(url);
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpDelete.setHeader(entry.getKey(), entry.getValue());
}
}
HttpResponse response = httpClient.execute(httpDelete);
String result = EntityUtils.toString(response.getEntity(), Charset.forName("UTF-8"));
map.put("response",response);
map.put("result",result);
return map;
}
default: {
throw new StatementException("参数缺失");
}
}
}
/**
* 发送资源
* @return
* @throws StatementException
*/
public Map<String,Object> sendResource() throws StatementException {
Map<String,Object> map = new HashMap<>();
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost httpPost = new HttpPost(url);
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
FileBody[] bins = new FileBody[filepath.length];
MultipartEntity reqEntity = new MultipartEntity();
for(int i = 0 ;i<filepath.length;i++){
bins[i] = new FileBody(new File(filepath[i]));
reqEntity.addPart(filename[i],bins[i]);
}
httpPost.setEntity(reqEntity);
HttpResponse responses = httpClient.execute(httpPost);
String result = EntityUtils.toString(responses.getEntity(), Charset.forName("UTF-8"));
map.put("response",responses);
map.put("result",result);
return map;
} catch (IOException e) {
e.printStackTrace();
throw new StatementException("參數缺失");
}
}
/**
* 获取资源
* @return
* @throws IOException
*/
public Map<String,Object>getResource() throws IOException {
Map<String,Object> map = new HashMap<>();
HttpGet httpGet = new HttpGet(url);
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
URL urls = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
conn.getContentType();
map.put("type",conn.getContentType());
map.put("stream",inStream);
return map;
}
private static DefaultHttpClient getHttpClient() {
try {
// 禁止https证书验证
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);//运行所有的hostname验证
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
// 禁用Cookie2请求头
HttpClientParams.setCookiePolicy(params, CookiePolicy.RFC_2109);
HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);
HttpClientParams.setCookiePolicy(params, CookiePolicy.NETSCAPE);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 5000);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
return new DefaultHttpClient(httpParams);
}
}
static class SSLSocketFactoryEx extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance("TLS");
public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[]{tm}, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
class StatementException extends Exception{
public StatementException(String msg)
{
super(msg);
}
}
}
demo
Map<String, Object> httpConnect = new HttpConnect.Builder()
.xmldata(xmldata)
.url(url)
.method(HttpConnect.POST)
.bulid().sendData();