fixed #25
This commit is contained in:
parent
9cf1dc5567
commit
5c212907fa
@ -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,
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -855,7 +855,6 @@ public class WeixinProxy {
|
||||
* @return 消息发送状态
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.api.MassApi
|
||||
* @see {@link com.foxinmy.weixin4j.util.MessageUtil#getStatusDesc(String)}
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E6.9F.A5.E8.AF.A2.E7.BE.A4.E5.8F.91.E6.B6.88.E6.81.AF.E5.8F.91.E9.80.81.E7.8A.B6.E6.80.81.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91">查询群发状态</a>
|
||||
*/
|
||||
|
||||
@ -54,8 +54,10 @@ public class MediaTest extends TokenTest {
|
||||
|
||||
@Test
|
||||
public void download1() throws WeixinException, IOException {
|
||||
MediaDownloadResult content = mediaApi.downloadMedia(
|
||||
"BmzcrM2jaJXZCjBqxLwyC03kh5pge3CbrBqXP4XbYBCeKr7xz-rHuwPf4vYLVdL1", false);
|
||||
MediaDownloadResult content = mediaApi
|
||||
.downloadMedia(
|
||||
"BmzcrM2jaJXZCjBqxLwyC03kh5pge3CbrBqXP4XbYBCeKr7xz-rHuwPf4vYLVdL1",
|
||||
false);
|
||||
Assert.assertTrue(content != null);
|
||||
System.err.println(content);
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.model.MediaDownloadResult;
|
||||
import com.foxinmy.weixin4j.model.MediaUploadResult;
|
||||
import com.foxinmy.weixin4j.qy.api.MediaApi;
|
||||
import com.foxinmy.weixin4j.type.MediaType;
|
||||
@ -34,8 +35,8 @@ public class MediaTest extends TokenTest {
|
||||
@Test
|
||||
public void upload() throws IOException, WeixinException {
|
||||
File file = new File("/Users/jy/Downloads/weixin4j.png");
|
||||
MediaUploadResult mediaResult = mediaApi.uploadMedia(0, new FileInputStream(
|
||||
file), file.getName());
|
||||
MediaUploadResult mediaResult = mediaApi.uploadMedia(0,
|
||||
new FileInputStream(file), file.getName());
|
||||
// 1gJ0vRLQp_o7L9hsVm6sviuGWc0qaPOd-KjkUZ6KQ7IrFVui8b2ZZd9F5szLCUkkD8gxk65lwW2SV72XO1RGZTQ
|
||||
Assert.assertNotNull(mediaResult.getMediaId());
|
||||
System.out.println(mediaResult);
|
||||
@ -54,4 +55,22 @@ public class MediaTest extends TokenTest {
|
||||
public void listAll() throws WeixinException {
|
||||
mediaApi.listAllMaterialMedia(1, MediaType.image);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void uploadFile() throws IOException, WeixinException {
|
||||
File file = new File("/Users/jy/Downloads/弹性运动1.html");
|
||||
MediaUploadResult mediaResult = mediaApi.uploadMedia(0,
|
||||
new FileInputStream(file), "弹性运动1.html");
|
||||
Assert.assertNotNull(mediaResult.getMediaId());
|
||||
System.out.println(mediaResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void downloadFile() throws WeixinException {
|
||||
MediaDownloadResult result = mediaApi
|
||||
.downloadMedia(
|
||||
0,
|
||||
"19pXNIq8cd69QLwfsLaoZFfS2K82WCHNGPREO--o1rEMlNIOf0N9IDDQdler08S7fNAFsG-5XYwxf1gzORxDnlQ");
|
||||
System.err.println(result);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user