packagecloud.app.prod.home.wechat;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.text.MessageFormat;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.Set;importorg.apache.http.HttpEntity;importorg.apache.http.HttpHost;importorg.apache.http.HttpResponse;importorg.apache.http.NameValuePair;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.HttpClient;importorg.apache.http.client.config.RequestConfig;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.methods.HttpUriRequest;importorg.apache.http.client.methods.RequestBuilder;importorg.apache.http.conn.routing.HttpRoute;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClientBuilder;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.impl.conn.PoolingHttpClientConnectionManager;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importorg.apache.log4j.Logger;/*** Author : YongBo Xie
* File Name: WechatAPIHander.java
* Created Date: 2018年3月13日 下午6:29:00
* Modified Date: 2018年3月13日 下午6:29:00
* Version: 1.0 */
public classDSHWechatAPIHander {private static Logger logger = Logger.getLogger(DSHWechatAPIHander.class);/*** 主动推送信息接口(群发)*/
private static String SEND_MSG_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={0}";private static PoolingHttpClientConnectionManager connectionManager = null;private static HttpClientBuilder httpBuilder = null;private static RequestConfig requestConfig = null;private static int MAXCONNECTION = 10;private static int DEFAULTMAXCONNECTION = 5;private static String IP = "127.0.0.1";private static int PORT = 8888;static{//设置http的状态参数
requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build();
HttpHost target= newHttpHost(IP, PORT);
connectionManager= newPoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(MAXCONNECTION);//客户端总并行链接最大数
connectionManager.setDefaultMaxPerRoute(DEFAULTMAXCONNECTION);//每个主机的最大并行链接数
connectionManager.setMaxPerRoute(new HttpRoute(target), 20);
httpBuilder=HttpClients.custom();
httpBuilder.setConnectionManager(connectionManager);
}public staticCloseableHttpClient getConnection() {
CloseableHttpClient httpClient=httpBuilder.build();returnhttpClient;
}public static HttpUriRequest getRequestMethod(Mapmap, String url, String method) {
List params = new ArrayList();
Set> entrySet =map.entrySet();for (Map.Entrye : entrySet) {
String name=e.getKey();
String value=e.getValue();
NameValuePair pair= newBasicNameValuePair(name, value);
params.add(pair);
}
HttpUriRequest reqMethod= null;if ("post".equals(method)) {
reqMethod= RequestBuilder.post().setUri(url).addParameters(params.toArray(newBasicNameValuePair[params.size()])).setConfig(requestConfig).build();
}else if ("get".equals(method)) {
reqMethod= RequestBuilder.get().setUri(url).addParameters(params.toArray(newBasicNameValuePair[params.size()])).setConfig(requestConfig).build();
}returnreqMethod;
}/*** @desc 推送信息
*@paramtoken
*@parammsg
*@return
*/
public static voidsendMessage(String token, String msg) {try{
logger.info("\n\nsendMessage start.token:" + token + ",msg:" +msg);
String url=MessageFormat.format(SEND_MSG_URL, token);//创建默认的httpClient实例.
CloseableHttpClient httpclient =HttpClients.createDefault();//创建httppost
HttpPost httppost = newHttpPost(url);//设置发送消息的参数//这里必须是一个合法的json格式数据,每个字段的意义可以查看上面连接的说明,content后面的test是要发送给用户的数据,这里是群发给所有人//msg = "{\"filter\":{\"is_to_all\":true},\"text\":{\"content\":\"test\"},\"msgtype\":\"text\"}\"";
StringEntity sEntity;try{
sEntity= newStringEntity(msg);//解决中文乱码的问题
sEntity.setContentEncoding("UTF-8");
sEntity.setContentType("application/json");
httppost.setEntity(sEntity);
System.out.println("executing request " +httppost.getURI());//发送请求
CloseableHttpResponse response =httpclient.execute(httppost);try{
HttpEntity hEntity=response.getEntity();if (hEntity != null) {
System.out.println("--------------------------------------");
System.out.println("Response content: " + EntityUtils.toString(hEntity, "UTF-8"));
System.out.println("--------------------------------------");
}
}finally{
response.close();
}
}catch(ClientProtocolException e) {
e.printStackTrace();
}catch(UnsupportedEncodingException e1) {
e1.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{//关闭连接,释放资源
try{
httpclient.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}catch(Exception e) {
logger.error("get user info exception", e);
}
}public static void main(String args[]) throwsIOException {
Map map = new HashMap();
map.put("account", "");
map.put("password", "");
HttpClient client=getConnection();
HttpUriRequest post= getRequestMethod(map, "http://baidu.com", "post");
HttpResponse response=client.execute(post);if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity=response.getEntity();
String message= EntityUtils.toString(entity, "utf-8");
System.out.println(message);
}else{
System.out.println("请求失败");
}
}
}