WeChatHttpClient.java
4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.phxl.modules.wechat.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.phxl.common.response.PlatFormResponse;
import com.phxl.common.response.PlatFormResponseConstant;
import com.phxl.common.util.JsonMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName WeChatHttpClient
* @DescriPtion 用于微信端调登陆和登出
* @Author 何
* @Date 2019-06-24 14:54
* @Version 1.0
**/
public class WeChatHttpClient {
private static HttpClient client;
private static Logger logger=LoggerFactory.getLogger(WeChatHttpClient.class);
//使用POST方法发送JSON数据
public static String sendLogin(String url, JSONObject userObject) throws Exception {
String result = "";
PlatFormResponse platFormResponse = new PlatFormResponse();
if (client == null){
client = HttpClients.createDefault();
}
try {
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
post.setHeader("Cookie","JSESSIONID="+userObject.getString("sessionId"));
//StringEntity entity1 = new StringEntity(jsonData,"UTF-8");
List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
list.add(new BasicNameValuePair("username", userObject.getString("username")));
list.add(new BasicNameValuePair("password", userObject.getString("password")));
list.add(new BasicNameValuePair("openId",userObject.getString("openId")));
post.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();//标准Cookie策略
client = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();//设置进去
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
platFormResponse.setFlag(PlatFormResponseConstant.ResponseBodyMsg.fail_ResultCode);
platFormResponse.setMsg(e.getMessage());
result = JSON.toJSONString(platFormResponse);
}
return result;
}
public static String sendLogout(String url,String token) {
String responseContent = "";
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Content-Type", "application/json");
httpGet.addHeader("Cookie","token="+token);
// httpGet.addHeader("Content-Type",
// "application/json;charset=utf-8");
CloseableHttpResponse response = httpClient.execute(httpGet);
System.out.println(response.getStatusLine().getStatusCode() + "\n");
HttpEntity entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseContent);
logger.info(url + " 返回结果:" + responseContent);
response.close();
httpClient.close();
} catch (Exception e) {
logger.error("调用:" + url + " 抛出异常:",e);
}
return responseContent;
}
//使用POST方法发送JSON数据
public static String sendPostData(String url, JSONObject object) throws Exception {
String result = "";
PlatFormResponse platFormResponse = new PlatFormResponse();
if (client == null){
client = HttpClients.createDefault();
}
try {
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "application/json");
post.setHeader("Cookie","token="+object.getString("token"));
StringEntity entity1 = new StringEntity(JsonMapper.toJsonString(object),"UTF-8");
post.setEntity(entity1);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
platFormResponse.setFlag(PlatFormResponseConstant.ResponseBodyMsg.fail_ResultCode);
platFormResponse.setMsg(e.getMessage());
result = JSON.toJSONString(platFormResponse);
}
return result;
}
}