fixed #25
This commit is contained in:
@@ -116,8 +116,9 @@ public class HttpComponent3 extends AbstractHttpClient {
|
||||
.setConnectionTimeout(params.getConnectTimeout());
|
||||
httpClient.getHttpConnectionManager().getParams()
|
||||
.setSendBufferSize(params.getChunkSize());
|
||||
httpMethod.getParams().setUriCharset(Consts.UTF_8.name());
|
||||
httpMethod.getParams().setSoTimeout(params.getSocketTimeout());
|
||||
httpMethod.getParams().setHttpElementCharset(Consts.UTF_8.name());
|
||||
httpMethod.getParams().setUriCharset(Consts.UTF_8.name());
|
||||
httpMethod.getParams().setContentCharset(Consts.UTF_8.name());
|
||||
}
|
||||
if (useSSL) {
|
||||
|
||||
@@ -70,6 +70,10 @@ public class HttpComponent4_1 extends HttpComponent4 {
|
||||
params.getConnectTimeout());
|
||||
uriRequest.getParams().setParameter(
|
||||
CoreProtocolPNames.HTTP_CONTENT_CHARSET, Consts.UTF_8);
|
||||
uriRequest.getParams().setParameter(
|
||||
CoreProtocolPNames.HTTP_ELEMENT_CHARSET, Consts.UTF_8.name());
|
||||
uriRequest.getParams().setParameter(
|
||||
CoreProtocolPNames.STRICT_TRANSFER_ENCODING, Consts.UTF_8);
|
||||
uriRequest.getParams().setParameter(
|
||||
HttpHeaders.CONTENT_ENCODING, Consts.UTF_8);
|
||||
uriRequest.getParams().setParameter(HttpHeaders.ACCEPT_CHARSET,
|
||||
|
||||
+13
-2
@@ -10,15 +10,18 @@ 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.HttpRequestBase;
|
||||
import org.apache.http.config.ConnectionConfig;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.X509HostnameVerifier;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
|
||||
import com.foxinmy.weixin4j.http.HttpClientException;
|
||||
import com.foxinmy.weixin4j.http.HttpParams;
|
||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||
import com.foxinmy.weixin4j.http.HttpResponse;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
|
||||
/**
|
||||
* Requires Apache HttpComponents 4.3 or higher
|
||||
@@ -34,7 +37,11 @@ public class HttpComponent4_2 extends HttpComponent4 {
|
||||
private CloseableHttpClient httpClient;
|
||||
|
||||
public HttpComponent4_2() {
|
||||
this.httpClient = HttpClients.createDefault();
|
||||
this.httpClient = HttpClientBuilder
|
||||
.create()
|
||||
.setDefaultConnectionConfig(
|
||||
ConnectionConfig.custom().setCharset(Consts.UTF_8)
|
||||
.build()).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,8 +81,12 @@ public class HttpComponent4_2 extends HttpComponent4 {
|
||||
if (hostnameVerifier == null) {
|
||||
hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
|
||||
}
|
||||
httpClient = HttpClients.custom()
|
||||
httpClient = HttpClients
|
||||
.custom()
|
||||
.setHostnameVerifier(hostnameVerifier)
|
||||
.setDefaultConnectionConfig(
|
||||
ConnectionConfig.custom()
|
||||
.setCharset(Consts.UTF_8).build())
|
||||
.setSslcontext(sslContext).build();
|
||||
}
|
||||
addHeaders(request.getHeaders(), uriRequest);
|
||||
|
||||
+2
-2
@@ -80,8 +80,8 @@ public class WeixinRequestExecutor {
|
||||
|
||||
public WeixinResponse post(String url, FormBodyPart... bodyParts)
|
||||
throws WeixinException {
|
||||
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT,
|
||||
null, Consts.UTF_8);
|
||||
MultipartEntity entity = new MultipartEntity(
|
||||
HttpMultipartMode.BROWSER_COMPATIBLE, null, Consts.UTF_8);
|
||||
for (FormBodyPart bodyPart : bodyParts) {
|
||||
entity.addPart(bodyPart);
|
||||
}
|
||||
|
||||
@@ -12,27 +12,32 @@ import com.foxinmy.weixin4j.http.HttpParams;
|
||||
import com.foxinmy.weixin4j.http.HttpRequest;
|
||||
import com.foxinmy.weixin4j.http.HttpResponse;
|
||||
import com.foxinmy.weixin4j.http.factory.HttpClientFactory;
|
||||
import com.foxinmy.weixin4j.http.factory.HttpComponent3Factory;
|
||||
import com.foxinmy.weixin4j.http.factory.HttpComponent4Factory;
|
||||
import com.foxinmy.weixin4j.http.factory.Netty4HttpClientFactory;
|
||||
import com.foxinmy.weixin4j.http.factory.SimpleHttpClientFactory;
|
||||
import com.foxinmy.weixin4j.util.IOUtil;
|
||||
|
||||
public class HttpClientTest {
|
||||
|
||||
static HttpRequest request = new HttpRequest(HttpMethod.GET,
|
||||
"https://www.baidu.com");
|
||||
"http://www.iteye.com/");
|
||||
static {
|
||||
HttpParams params = new HttpParams();
|
||||
params.setProxy(new Proxy(Type.HTTP, new InetSocketAddress(
|
||||
"117.136.234.9", 80)));
|
||||
request.setParams(params);
|
||||
//request.setParams(params);
|
||||
}
|
||||
|
||||
public static void test1() throws HttpClientException {
|
||||
HttpClientFactory.setDefaultFactory(new SimpleHttpClientFactory());
|
||||
HttpClient httpClient = HttpClientFactory.getInstance();
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
print(response);
|
||||
}
|
||||
|
||||
public static void test2() throws HttpClientException {
|
||||
HttpClientFactory.setDefaultFactory(new HttpComponent3Factory());
|
||||
HttpClient httpClient = HttpClientFactory.getInstance();
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
print(response);
|
||||
@@ -45,25 +50,29 @@ public class HttpClientTest {
|
||||
print(response);
|
||||
}
|
||||
|
||||
public static void test4() throws HttpClientException {
|
||||
HttpClientFactory.setDefaultFactory(new Netty4HttpClientFactory());
|
||||
HttpClient httpClient = HttpClientFactory.getInstance();
|
||||
HttpResponse response = httpClient.execute(request);
|
||||
print(response);
|
||||
}
|
||||
|
||||
public static void print(HttpResponse response) throws HttpClientException {
|
||||
System.err.println(response.getStatus());
|
||||
try {
|
||||
System.err.println(new String(
|
||||
IOUtil.toByteArray(response.getBody())));
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.err.println(response.getHeaders());
|
||||
System.err.println(response.getProtocol());
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
test1();
|
||||
System.out.println("---------------------");
|
||||
// test2();
|
||||
test2();
|
||||
System.out.println("---------------------");
|
||||
// test3();
|
||||
// test4();
|
||||
test3();
|
||||
System.out.println("---------------------");
|
||||
test4();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user