新增HttpComponent4.java
This commit is contained in:
parent
dd186abd09
commit
6f892ebbf4
@ -0,0 +1,89 @@
|
||||
package com.foxinmy.weixin4j.http.factory;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpHead;
|
||||
import org.apache.http.client.methods.HttpOptions;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.client.methods.HttpTrace;
|
||||
import org.apache.http.entity.AbstractHttpEntity;
|
||||
import org.apache.http.entity.InputStreamEntity;
|
||||
|
||||
import com.foxinmy.weixin4j.http.AbstractHttpClient;
|
||||
import com.foxinmy.weixin4j.http.HttpHeaders;
|
||||
import com.foxinmy.weixin4j.http.HttpMethod;
|
||||
import com.foxinmy.weixin4j.http.apache.MultipartEntity;
|
||||
import com.foxinmy.weixin4j.http.entity.HttpEntity;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* Apache HttpComponents 4.x
|
||||
*
|
||||
* @className HttpComponent4
|
||||
* @author jy
|
||||
* @date 2015年8月18日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public abstract class HttpComponent4 extends AbstractHttpClient {
|
||||
protected static final Map<HttpMethod, HttpRequestBase> methodMap;
|
||||
static {
|
||||
methodMap = new HashMap<HttpMethod, HttpRequestBase>();
|
||||
methodMap.put(HttpMethod.GET, new HttpGet());
|
||||
methodMap.put(HttpMethod.HEAD, new HttpHead());
|
||||
methodMap.put(HttpMethod.POST, new HttpPost());
|
||||
methodMap.put(HttpMethod.PUT, new HttpPut());
|
||||
methodMap.put(HttpMethod.DELETE, new HttpDelete());
|
||||
methodMap.put(HttpMethod.OPTIONS, new HttpOptions());
|
||||
methodMap.put(HttpMethod.TRACE, new HttpTrace());
|
||||
}
|
||||
|
||||
protected void addHeaders(HttpHeaders headers, HttpRequestBase uriRequest) {
|
||||
if (headers != null) {
|
||||
for (Iterator<Entry<String, List<String>>> headerIterator = headers
|
||||
.entrySet().iterator(); headerIterator.hasNext();) {
|
||||
Entry<String, List<String>> header = headerIterator.next();
|
||||
if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
|
||||
uriRequest.addHeader(header.getKey(),
|
||||
StringUtil.join(header.getValue(), ';'));
|
||||
} else {
|
||||
for (String headerValue : header.getValue()) {
|
||||
uriRequest.addHeader(header.getKey(),
|
||||
headerValue != null ? headerValue : "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void addEntity(HttpEntity entity, HttpRequestBase uriRequest)
|
||||
throws IOException {
|
||||
if (entity != null) {
|
||||
AbstractHttpEntity httpEntity = null;
|
||||
if (entity instanceof MultipartEntity) {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
entity.writeTo(os);
|
||||
os.flush();
|
||||
httpEntity = new org.apache.http.entity.ByteArrayEntity(
|
||||
os.toByteArray());
|
||||
os.close();
|
||||
} else {
|
||||
httpEntity = new InputStreamEntity(entity.getContent(),
|
||||
entity.getContentLength());
|
||||
}
|
||||
httpEntity.setContentType(entity.getContentType().toString());
|
||||
((HttpEntityEnclosingRequestBase) uriRequest).setEntity(httpEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,45 +1,24 @@
|
||||
package com.foxinmy.weixin4j.http.factory;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpHead;
|
||||
import org.apache.http.client.methods.HttpOptions;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.client.methods.HttpTrace;
|
||||
import org.apache.http.conn.params.ConnRoutePNames;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||
import org.apache.http.entity.AbstractHttpEntity;
|
||||
import org.apache.http.entity.InputStreamEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.params.CoreConnectionPNames;
|
||||
import org.apache.http.params.CoreProtocolPNames;
|
||||
|
||||
import com.foxinmy.weixin4j.http.AbstractHttpClient;
|
||||
import com.foxinmy.weixin4j.http.HttpClient;
|
||||
import com.foxinmy.weixin4j.http.HttpClientException;
|
||||
import com.foxinmy.weixin4j.http.HttpHeaders;
|
||||
import com.foxinmy.weixin4j.http.HttpMethod;
|
||||
import com.foxinmy.weixin4j.http.HttpParams;
|
||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||
import com.foxinmy.weixin4j.http.HttpResponse;
|
||||
import com.foxinmy.weixin4j.http.apache.MultipartEntity;
|
||||
import com.foxinmy.weixin4j.http.entity.HttpEntity;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* Requires Apache HttpComponents 4.2 or lower
|
||||
@ -50,21 +29,12 @@ import com.foxinmy.weixin4j.util.StringUtil;
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class HttpComponent4_1 extends AbstractHttpClient implements HttpClient {
|
||||
public class HttpComponent4_1 extends HttpComponent4 implements HttpClient {
|
||||
|
||||
private final org.apache.http.client.HttpClient httpClient;
|
||||
private final Map<HttpMethod, HttpRequestBase> methodMap;
|
||||
|
||||
public HttpComponent4_1() {
|
||||
this.httpClient = new DefaultHttpClient();
|
||||
this.methodMap = new HashMap<HttpMethod, HttpRequestBase>();
|
||||
methodMap.put(HttpMethod.GET, new HttpGet());
|
||||
methodMap.put(HttpMethod.HEAD, new HttpHead());
|
||||
methodMap.put(HttpMethod.POST, new HttpPost());
|
||||
methodMap.put(HttpMethod.PUT, new HttpPut());
|
||||
methodMap.put(HttpMethod.DELETE, new HttpDelete());
|
||||
methodMap.put(HttpMethod.OPTIONS, new HttpOptions());
|
||||
methodMap.put(HttpMethod.TRACE, new HttpTrace());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -106,40 +76,8 @@ public class HttpComponent4_1 extends AbstractHttpClient implements HttpClient {
|
||||
.register(scheme);
|
||||
}
|
||||
}
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
if (headers != null) {
|
||||
for (Iterator<Entry<String, List<String>>> headerIterator = headers
|
||||
.entrySet().iterator(); headerIterator.hasNext();) {
|
||||
Entry<String, List<String>> header = headerIterator.next();
|
||||
if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
|
||||
uriRequest.addHeader(header.getKey(),
|
||||
StringUtil.join(header.getValue(), ';'));
|
||||
} else {
|
||||
for (String headerValue : header.getValue()) {
|
||||
uriRequest.addHeader(header.getKey(),
|
||||
headerValue != null ? headerValue : "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
HttpEntity entity = request.getEntity();
|
||||
if (entity != null) {
|
||||
AbstractHttpEntity httpEntity = null;
|
||||
if (entity instanceof MultipartEntity) {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
entity.writeTo(os);
|
||||
os.flush();
|
||||
httpEntity = new org.apache.http.entity.ByteArrayEntity(
|
||||
os.toByteArray());
|
||||
os.close();
|
||||
} else {
|
||||
httpEntity = new InputStreamEntity(entity.getContent(),
|
||||
entity.getContentLength());
|
||||
}
|
||||
httpEntity.setContentType(entity.getContentType().toString());
|
||||
((HttpEntityEnclosingRequestBase) uriRequest)
|
||||
.setEntity(httpEntity);
|
||||
}
|
||||
addHeaders(request.getHeaders(), uriRequest);
|
||||
addEntity(request.getEntity(), uriRequest);
|
||||
org.apache.http.HttpResponse httpResponse = httpClient
|
||||
.execute(uriRequest);
|
||||
response = new HttpComponent4_1Response(httpClient, httpResponse);
|
||||
|
||||
@ -1,44 +1,21 @@
|
||||
package com.foxinmy.weixin4j.http.factory;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.config.RequestConfig.Builder;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpHead;
|
||||
import org.apache.http.client.methods.HttpOptions;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.client.methods.HttpTrace;
|
||||
import org.apache.http.entity.AbstractHttpEntity;
|
||||
import org.apache.http.entity.InputStreamEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
|
||||
import com.foxinmy.weixin4j.http.AbstractHttpClient;
|
||||
import com.foxinmy.weixin4j.http.HttpClient;
|
||||
import com.foxinmy.weixin4j.http.HttpClientException;
|
||||
import com.foxinmy.weixin4j.http.HttpHeaders;
|
||||
import com.foxinmy.weixin4j.http.HttpMethod;
|
||||
import com.foxinmy.weixin4j.http.HttpParams;
|
||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||
import com.foxinmy.weixin4j.http.HttpResponse;
|
||||
import com.foxinmy.weixin4j.http.apache.MultipartEntity;
|
||||
import com.foxinmy.weixin4j.http.entity.HttpEntity;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
|
||||
/**
|
||||
* Requires Apache HttpComponents 4.3 or higher
|
||||
@ -49,21 +26,12 @@ import com.foxinmy.weixin4j.util.StringUtil;
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class HttpComponent4_2 extends AbstractHttpClient implements HttpClient {
|
||||
public class HttpComponent4_2 extends HttpComponent4 implements HttpClient {
|
||||
|
||||
private CloseableHttpClient httpClient;
|
||||
private final Map<HttpMethod, HttpRequestBase> methodMap;
|
||||
|
||||
public HttpComponent4_2() {
|
||||
this.httpClient = HttpClients.createDefault();
|
||||
this.methodMap = new HashMap<HttpMethod, HttpRequestBase>();
|
||||
methodMap.put(HttpMethod.GET, new HttpGet());
|
||||
methodMap.put(HttpMethod.HEAD, new HttpHead());
|
||||
methodMap.put(HttpMethod.POST, new HttpPost());
|
||||
methodMap.put(HttpMethod.PUT, new HttpPut());
|
||||
methodMap.put(HttpMethod.DELETE, new HttpDelete());
|
||||
methodMap.put(HttpMethod.OPTIONS, new HttpOptions());
|
||||
methodMap.put(HttpMethod.TRACE, new HttpTrace());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -91,40 +59,8 @@ public class HttpComponent4_2 extends AbstractHttpClient implements HttpClient {
|
||||
}
|
||||
uriRequest.setConfig(requestConfig.build());
|
||||
}
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
if (headers != null) {
|
||||
for (Iterator<Entry<String, List<String>>> headerIterator = headers
|
||||
.entrySet().iterator(); headerIterator.hasNext();) {
|
||||
Entry<String, List<String>> header = headerIterator.next();
|
||||
if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
|
||||
uriRequest.addHeader(header.getKey(),
|
||||
StringUtil.join(header.getValue(), ';'));
|
||||
} else {
|
||||
for (String headerValue : header.getValue()) {
|
||||
uriRequest.addHeader(header.getKey(),
|
||||
headerValue != null ? headerValue : "");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
HttpEntity entity = request.getEntity();
|
||||
if (entity != null) {
|
||||
AbstractHttpEntity httpEntity = null;
|
||||
if (entity instanceof MultipartEntity) {
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
entity.writeTo(os);
|
||||
os.flush();
|
||||
httpEntity = new org.apache.http.entity.ByteArrayEntity(
|
||||
os.toByteArray());
|
||||
os.close();
|
||||
} else {
|
||||
httpEntity = new InputStreamEntity(entity.getContent(),
|
||||
entity.getContentLength());
|
||||
}
|
||||
httpEntity.setContentType(entity.getContentType().toString());
|
||||
((HttpEntityEnclosingRequestBase) uriRequest)
|
||||
.setEntity(httpEntity);
|
||||
}
|
||||
addHeaders(request.getHeaders(), uriRequest);
|
||||
addEntity(request.getEntity(), uriRequest);
|
||||
CloseableHttpResponse httpResponse = httpClient.execute(uriRequest);
|
||||
response = new HttpComponent4_2Response(httpResponse);
|
||||
} catch (IOException e) {
|
||||
|
||||
@ -8,7 +8,6 @@ import org.apache.http.HttpEntity;
|
||||
import org.apache.http.ProtocolVersion;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import com.foxinmy.weixin4j.http.HttpClientException;
|
||||
import com.foxinmy.weixin4j.http.HttpHeaders;
|
||||
|
||||
@ -20,7 +20,6 @@ import com.foxinmy.weixin4j.http.entity.FormUrlEntity;
|
||||
import com.foxinmy.weixin4j.http.entity.HttpEntity;
|
||||
import com.foxinmy.weixin4j.http.entity.StringEntity;
|
||||
import com.foxinmy.weixin4j.http.factory.HttpClientFactory;
|
||||
import com.foxinmy.weixin4j.http.factory.HttpComponent4Factory;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
import com.foxinmy.weixin4j.util.WeixinErrorUtil;
|
||||
@ -45,7 +44,6 @@ public class WeixinRequestExecutor {
|
||||
}
|
||||
|
||||
public WeixinRequestExecutor(HttpParams params) {
|
||||
HttpClientFactory.setDefaultFactory(new HttpComponent4Factory());
|
||||
this.httpClient = HttpClientFactory.getInstance();
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
@ -74,8 +74,7 @@ public class MenuTest extends TokenTest {
|
||||
for (Button btn : btnList) {
|
||||
System.out.println(btn);
|
||||
}
|
||||
System.err.println(menuApi.getMenu());
|
||||
Assert.assertEquals(1, btnList.size());
|
||||
Assert.assertEquals(3, btnList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user