This commit is contained in:
jinyu
2016-06-11 11:31:37 +08:00
parent 57daea99a8
commit 31a66335a8
11 changed files with 126 additions and 179 deletions
@@ -16,82 +16,121 @@ import javax.net.ssl.SSLContext;
*/
public final class HttpParams {
private final boolean allowUserInteraction;
private final int connectTimeout;
private final int socketTimeout;
private final int readTimeout;
private final int chunkSize;
private final boolean followRedirects;
private boolean allowUserInteraction;
private int connectTimeout;
private int socketTimeout;
private int readTimeout;
private int chunkSize;
private boolean followRedirects;
/**
* 代理对象
*/
private final Proxy proxy;
private Proxy proxy;
/**
* SSL对象
*/
private final SSLContext sslContext;
private SSLContext sslContext;
/**
* hostname对象
*/
private final HostnameVerifier hostnameVerifier;
private HostnameVerifier hostnameVerifier;
HttpParams(boolean allowUserInteraction, int connectTimeout,
int socketTimeout, int readTimeout, int chunkSize,
boolean followRedirects, Proxy proxy, SSLContext sslContext,
HostnameVerifier hostnameVerifier) {
this.allowUserInteraction = allowUserInteraction;
public HttpParams() {
this(5000, 5000, 5000);
}
public HttpParams(int connectTimeout, int socketTimeout, int readTimeout) {
this.allowUserInteraction = true;
this.connectTimeout = connectTimeout;
this.socketTimeout = socketTimeout;
this.readTimeout = readTimeout;
this.chunkSize = chunkSize;
this.followRedirects = followRedirects;
this.proxy = proxy;
this.sslContext = sslContext;
this.hostnameVerifier = hostnameVerifier;
this.chunkSize = 4096;
this.followRedirects = false;
}
public boolean isAllowUserInteraction() {
return allowUserInteraction;
}
public HttpParams setAllowUserInteraction(boolean allowUserInteraction) {
this.allowUserInteraction = allowUserInteraction;
return this;
}
public int getConnectTimeout() {
return connectTimeout;
}
public HttpParams setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public int getSocketTimeout() {
return socketTimeout;
}
public HttpParams setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
return this;
}
public int getReadTimeout() {
return readTimeout;
}
public HttpParams setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public int getChunkSize() {
return chunkSize;
}
public HttpParams setChunkSize(int chunkSize) {
this.chunkSize = chunkSize;
return this;
}
public boolean isFollowRedirects() {
return followRedirects;
}
public HttpParams setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}
public Proxy getProxy() {
return proxy;
}
public HttpParams setProxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
public SSLContext getSSLContext() {
return sslContext;
}
public HttpParams setSSLContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}
public HostnameVerifier getHostnameVerifier() {
return hostnameVerifier;
}
public static HttpParams.Builder custom() {
return new Builder();
public HttpParams setHostnameVerifier(HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
return this;
}
public static HttpParams.Builder copy(final HttpParams params) {
return new Builder()
public static HttpParams copy(final HttpParams params) {
return new HttpParams()
.setAllowUserInteraction(params.isAllowUserInteraction())
.setConnectTimeout(params.getConnectTimeout())
.setSocketTimeout(params.getSocketTimeout())
@@ -100,120 +139,13 @@ public final class HttpParams {
.setFollowRedirects(params.isFollowRedirects());
}
public static class Builder {
private boolean allowUserInteraction;
private int connectTimeout;
private int socketTimeout;
private int readTimeout;
private int chunkSize;
private boolean followRedirects;
/**
* 代理对象
*/
private Proxy proxy;
/**
* SSL对象
*/
private SSLContext sslContext;
/**
* hostname对象
*/
private HostnameVerifier hostnameVerifier;
Builder() {
this.allowUserInteraction = true;
this.connectTimeout = 5000;
this.socketTimeout = 5000;
this.readTimeout = 5000;
this.chunkSize = 4096;
this.followRedirects = false;
}
public boolean isAllowUserInteraction() {
return allowUserInteraction;
}
public Builder setAllowUserInteraction(boolean allowUserInteraction) {
this.allowUserInteraction = allowUserInteraction;
return this;
}
public int getConnectTimeout() {
return connectTimeout;
}
public Builder setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
public int getSocketTimeout() {
return socketTimeout;
}
public Builder setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
return this;
}
public int getReadTimeout() {
return readTimeout;
}
public Builder setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
public int getChunkSize() {
return chunkSize;
}
public Builder setChunkSize(int chunkSize) {
this.chunkSize = chunkSize;
return this;
}
public boolean isFollowRedirects() {
return followRedirects;
}
public Builder setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
return this;
}
public Proxy getProxy() {
return proxy;
}
public Builder setProxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
public SSLContext getSslContext() {
return sslContext;
}
public Builder setSslContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}
public HostnameVerifier getHostnameVerifier() {
return hostnameVerifier;
}
public Builder setHostnameVerifier(HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
return this;
}
public HttpParams build() {
return new HttpParams(allowUserInteraction, connectTimeout,
socketTimeout, readTimeout, chunkSize, followRedirects,
proxy, sslContext, hostnameVerifier);
}
@Override
public String toString() {
return "HttpParams [allowUserInteraction=" + allowUserInteraction
+ ", connectTimeout=" + connectTimeout + ", socketTimeout="
+ socketTimeout + ", readTimeout=" + readTimeout
+ ", chunkSize=" + chunkSize + ", followRedirects="
+ followRedirects + ", proxy=" + proxy + ", sslContext="
+ sslContext + ", hostnameVerifier=" + hostnameVerifier + "]";
}
}
@@ -117,8 +117,7 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
}
logger.debug("request >> " + request.getMethod() + " "
+ request.getURI().toString());
for (Entry<String, List<String>> header : headers
.entrySet()) {
for (Entry<String, List<String>> header : headers.entrySet()) {
if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
connection.setRequestProperty(header.getKey(),
StringUtil.join(header.getValue(), ';'));
@@ -170,8 +169,8 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
response = new SimpleHttpResponse(connection, content);
logger.debug("response << " + response.getProtocol()
+ response.getStatus().toString());
for (Entry<String, List<String>> header : response
.getHeaders().entrySet()) {
for (Entry<String, List<String>> header : response.getHeaders()
.entrySet()) {
logger.debug("headers << " + header.getKey() + ":"
+ StringUtil.join(header.getValue(), ';'));
}
@@ -38,11 +38,11 @@ public class WeixinRequestExecutor {
protected final InternalLogger logger = InternalLoggerFactory
.getInstance(getClass());
private final HttpClient httpClient;
private final HttpParams httpParams;
protected final HttpClient httpClient;
protected final HttpParams httpParams;
public WeixinRequestExecutor() {
this(HttpParams.custom().build());
this(new HttpParams());
}
public WeixinRequestExecutor(HttpParams httpParams) {
@@ -7,7 +7,6 @@ import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.HttpParams;
import com.foxinmy.weixin4j.http.HttpRequest;
import com.foxinmy.weixin4j.model.Consts;
@@ -51,14 +50,7 @@ public class WeixinSSLRequestExecutor extends WeixinRequestExecutor {
@Override
protected WeixinResponse doRequest(HttpRequest request)
throws WeixinException {
HttpParams params = null;
if (request.getParams() != null) {
params = HttpParams.copy(request.getParams())
.setSslContext(sslContext).build();
} else {
params = HttpParams.custom().setSslContext(sslContext).build();
}
request.setParams(params);
httpParams.setSSLContext(sslContext);
return super.doRequest(request);
}
}
@@ -8,7 +8,7 @@ import com.foxinmy.weixin4j.type.MediaType;
/**
* 媒体素材记录
*
*
* @className MediaRecord
* @author jinyu(foxinmy@gmail.com)
* @date 2015年3月22日
@@ -37,7 +37,7 @@ public class MediaRecord implements Serializable {
/**
* 媒体信息
*/
@JSONField(name = "itemlist")
@JSONField(name = "items")
private List<MediaItem> items;
/**
* 分页信息
@@ -23,11 +23,8 @@ public class HttpClientTest {
static HttpRequest request = new HttpRequest(HttpMethod.GET,
"http://www.iteye.com/");
static {
HttpParams params = HttpParams
.custom()
.setProxy(
new Proxy(Type.HTTP, new InetSocketAddress(
"117.136.234.9", 80))).build();
HttpParams params = new HttpParams().setProxy(new Proxy(Type.HTTP,
new InetSocketAddress("117.136.234.9", 80)));
// request.setParams(params);
}