98b0fe18e5ea4689373062984b13dc8cba6b2769.svn-base
6.88 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.thinkgem.jeesite.modules.title.buss;
import java.io.IOException;
import java.net.ConnectException;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
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.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.exception.BussException;
import com.thinkgem.jeesite.common.utils.HttpUtil;
import com.thinkgem.jeesite.common.utils.JsonUtil;
import com.thinkgem.jeesite.common.utils.StringUtil;
import com.thinkgem.jeesite.common.utils.Sysutil;
@Component
public class RemoteBuss {
private static final Logger logger = LoggerFactory.getLogger(RemoteBuss.class);
public void sendEvent(Map data) throws Exception {
String url = Global.getTitleUrl() + "/add/event";
HttpUtil httpUtil = new HttpUtil();
Map params = Maps.newHashMap();
params.put("data",JsonUtil.toJsonString(data));
String resmsg = httpUtil.getReturnResByUrl(Global.getTitleUrl(), url,params,null);
Map resmap = JsonUtil.jsonToMap(resmsg);
String errcode = StringUtil.getValueByMap(resmap, "resultCode", "0");
if("0".equals(errcode)) {
logger.info("事件转发完成");
}else {
String errmsg = resmap.get("errmsg")== null?"": resmap.get("errmsg").toString();
throw new Exception("事件转发失败,错误信息:"+errmsg);
}
}
public Map getZdxx() throws Exception {
HttpUtil httpUtil = new HttpUtil();
String url = "/get/zdxx";
Map map = Maps.newHashMap();
map.put("qxdm", Global.getAreaCode());
String resmsg = httpUtil.getReturnResByUrl(Global.getTitleUrl(), url,map,null);
logger.info(resmsg);
Map resmap = JsonUtil.jsonToMap(resmsg);
String errcode = StringUtil.getValueByMap(resmap, "resultCode", "0");
if("0".equals(errcode)) {
logger.info("完成");
return resmap;
}else {
String errmsg = resmap.get("resultMsg")== null?"": resmap.get("resultMsg").toString();
logger.error("失败,错误信息:"+errmsg);
throw new Exception(errmsg);
}
}
public Map getZxx() throws Exception {
HttpUtil httpUtil = new HttpUtil();
String url = "/get/zxx";
Map map = Maps.newHashMap();
map.put("qxdm", Global.getAreaCode());
String resmsg = httpUtil.getReturnResByUrl(Global.getTitleUrl(), url,map,null);
Map resmap = JsonUtil.jsonToMap(resmsg);
String errcode = StringUtil.getValueByMap(resmap, "resultCode", "0");
if("0".equals(errcode)) {
logger.info("完成");
return resmap;
}else {
String errmsg = resmap.get("resultMsg")== null?"": resmap.get("resultMsg").toString();
logger.error("失败,错误信息:"+errmsg);
throw new Exception(errmsg);
}
}
// post方式向权籍端传递数据
public String posturl(Map params, String url) throws IOException {
logger.debug("222"+params);
Integer URLOUTTIME = 1000*60*5;
String res = "";
HttpPost httppost = null;
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
Exception exception = null;
try {
client = HttpClientBuilder.create().build();
httppost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(URLOUTTIME)
.setConnectTimeout(URLOUTTIME).setSocketTimeout(URLOUTTIME).build();
httppost.setConfig(requestConfig);
List<NameValuePair> urlParameters = Lists.newArrayList();
if(params != null && params.size()>0) {
Iterator<String> it = params.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
urlParameters.add(new BasicNameValuePair(key,params.get(key).toString()));
}
httppost.setEntity(new UrlEncodedFormEntity(urlParameters, "utf-8"));
}
httppost.setHeader("Connection", "Keep-Alive");
httppost.setHeader("Accept", "*/*");
httppost.setHeader("Cookie","yikikata=12ea5adf-6c4a9902f1d8a258fa573024717cb369; brmidyrvj=both");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
response = client.execute(httppost);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
res = EntityUtils.toString(response.getEntity(), "utf-8");
}else if (code == 307) {
Header header_location = response.getFirstHeader("location"); // 跳转的目标地址是在 HTTP-HEAD上
String newuri = header_location.getValue(); // 这就是跳转后的地址,再向这个地址发出新申请
logger.info("307 url:"+url);
Header header_cookie = response.getFirstHeader("Set-Cookie");
String cookie = header_cookie+ "; brmidyrvj=both";
logger.info("307 cookie:"+cookie);
httppost.setURI(new URI(url));
httppost.setHeader("Cookie",cookie);
response = client.execute(httppost);
code = response.getStatusLine().getStatusCode();
if (code == 200) {
res = EntityUtils.toString(response.getEntity(), "utf-8");
}else {
throw new BussException("返回状态代码" + code);
}
} else {
throw new BussException("返回状态代码" + code);
}
}catch (ConnectException s) {
logger.info("http请求发生错误",s);
throw new BussException("网络连接错误");
} catch (Exception s) {
logger.info("http请求发生错误",s);
throw new BussException(" http请求发生错误," + s.getMessage());
} finally {
if (httppost != null) {
httppost.releaseConnection();
}
if (response != null) {
response.close();
}
if (client != null) {
client.close();
}
}
return res;
}
public Map getZbxx(String bdcdyh) throws Exception {
HttpUtil httpUtil = new HttpUtil();
String url = "/get/zbxx";
Map map = Maps.newHashMap();
map.put("bdcdyh",bdcdyh);
String resmsg = httpUtil.getReturnResByUrl(Global.getTitleUrl(), url,map,null);
logger.info(resmsg);
Map resmap = JsonUtil.jsonToMap(resmsg);
String errcode = StringUtil.getValueByMap(resmap, "resultCode", "0");
if("0".equals(errcode)) {
logger.info("完成");
return resmap;
}else {
String errmsg = resmap.get("resultMsg")== null?"": resmap.get("resultMsg").toString();
logger.error("失败,错误信息:"+errmsg);
throw new Exception(errmsg);
}
}
}