废话不多说直接直接贴代码:
调用的时候 直接调 doConnect 方法即可
Map map = new HashMap();
map.put("unitid", systemType);
List<Map<String, Object>> lstGpsData = WebServiceRequest.doConnect("xxcWorkStatisticsReport", map);
1 package com.csnt.scdp.bizmodules.modules.plcj.common.helper;
2
3
4 import com.csnt.scdp.framework.util.JsonUtil;
5 import com.csnt.scdp.sysmodules.helper.FMCodeHelper;
6 import org.apache.xerces.dom.DeferredTextImpl;
7 import org.apache.commons.codec.binary.Base64;
8 import org.json.JSONException;
9 import org.w3c.dom.Document;
10 import org.w3c.dom.Element;
11 import org.w3c.dom.Node;
12 import org.w3c.dom.NodeList;
13 import org.xml.sax.InputSource;
14
15 import javax.xml.parsers.DocumentBuilder;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import java.io.*;
18 import java.net.HttpURLConnection;
19 import java.net.URL;
20 import java.security.MessageDigest;
21 import java.security.NoSuchAlgorithmException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25
26 /**
27 * Created by on 2017/5/16.
28 */
29 public class WebServiceRequest {
30
31
32 public static void main(String[] args) throws Exception {
33 String userName = "SYSADMIN";//用户名
34 String password = "SYSADMIN";//密码
35 String host = "localhost:8282";
36 String dataTemplate = "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <m:hbzptInfo xmlns:m="http://intf.webservices.bizmodules.scdp.csnt" +
37 ".com/"> <arg0>#funName</arg0> <arg1>#param</arg1> </m:hbzptInfo > " +
38 "</SOAP-ENV:Body> </SOAP-ENV:Envelope> ";
39 // 接口名称
40 dataTemplate = dataTemplate.replaceAll("#funName", "bridgeHoleInfo");
41 // 请求参数
42 dataTemplate = dataTemplate.replaceAll("#param", "{}");
43 List<Map<String, Object>> rlt = getWebserviceResponse("http://localhost:8282/services/BizWebService", "POST", dataTemplate, null);
44
45 System.out.println(rlt.size());
46
47 }
48
49 public static List doConnect(String funName, Map params) {
50 List<Map<String, Object>> result = new ArrayList<>();
51 try {
52 String url = FMCodeHelper.getDescByCode("SERVICE_IP", "CLGSERVICES");
53 // String url = "http://172.10.50.135:8080/services/";
54 // url = "http://你的接口地址:6180/hbzs/services/";
55
56 String dataTemplate = "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <m:hbzptInfo xmlns:m="http://intf.webservices.bizmodules.scdp.csnt" +
57 ".com/"> <arg0>#funName</arg0> <arg1>#param</arg1> </m:hbzptInfo > " +
58 "</SOAP-ENV:Body> </SOAP-ENV:Envelope> ";
59 // 接口名称
60 dataTemplate = dataTemplate.replaceAll("#funName", funName);
61 // 请求参数
62 dataTemplate = dataTemplate.replaceAll("#param", JsonUtil.serialize(params));
63 // dataTemplate = dataTemplate.replaceAll("#param",java.net.URLEncoder.encode(JsonUtil.serialize(params),"GBK"));
64 result = getWebserviceResponse(url, "POST", dataTemplate, null);
65 } catch (Exception e) {
66 e.printStackTrace();
67 }
68 return result;
69 }
70
71 private static List getWebserviceResponse(String url, String method, String data, String header) throws
72 NoSuchAlgorithmException, IOException, JSONException {
73 byte[] buf = data.getBytes();
74 // 2 创建URL:
75 URL uri = new URL(url + "BizWebService/");
76 // 3 建立连接,并将连接强转为Http连接
77 HttpURLConnection httpConn = (HttpURLConnection) uri.openConnection();
78 httpConn.setRequestMethod(method);
79 httpConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
80 httpConn.setRequestProperty("Accept", "Accept=[*/*]");
81 httpConn.setRequestProperty("accept-encoding", "UTF-8");
82 httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
83 if (header != null && !"".equals(header)) {
84 httpConn.setRequestProperty("Authorization", header);
85 }
86 // String userName = FMCodeHelper.getDescByCode("USERNAME", "CLGSERVICES");
87 String userName = "INTFUSER-PLCJ";
88 httpConn.setRequestProperty("Authorization", "Basic " + Base64.encodeBase64String((userName + ":" + doLogin(url + "Login/")).getBytes("UTF-8")));
89 // 4,设置请求方式和请求头:
90 httpConn.setDoOutput(true); // 是否有入参
91 httpConn.setDoInput(true);// 是否有出参
92 httpConn.setRequestProperty("contentType", "UTF8");
93 httpConn.setRequestProperty("Accept-Charset", "UTF8");
94 OutputStream out = httpConn.getOutputStream();
95 out.write(buf);
96 out.close();
97 int code = httpConn.getResponseCode();
98 StringBuilder sb = new StringBuilder("");
99 if (code == HttpURLConnection.HTTP_OK) {
100 InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), "utf-8");
101 BufferedReader br = new BufferedReader(isr);
102 sb = new StringBuilder();
103 String str;
104 while ((str = br.readLine()) != null) {
105 sb.append(str);
106 }
107 br.close();
108 isr.close();
109 } else {
110 InputStream retStream = httpConn.getErrorStream();
111 if(retStream!=null){
112 InputStreamReader isr = new InputStreamReader(retStream, "utf-8");
113 BufferedReader br = new BufferedReader(isr);
114 sb = new StringBuilder();
115 String str;
116 while ((str = br.readLine()) != null) {
117 sb.append(str).append("
");
118 }
119 br.close();
120 isr.close();
121 }
122 }
123 return parserXML(sb.toString());
124 }
125
126 public static List parserXML(String strXML) {
127 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
128 List results = new ArrayList();
129 try {
130 // 解析xml
131 DocumentBuilder builder = factory.newDocumentBuilder();
132 StringReader sr = new StringReader(strXML);
133 InputSource is = new InputSource(sr);
134 // 使用W3C的方式解析xml
135 Document doc = builder.parse(is);
136 Element rootElement = doc.getDocumentElement();
137 NodeList phones = rootElement.getElementsByTagName("return");
138 Node type = phones.item(0);
139
140 NodeList properties = type.getChildNodes();
141 Node property = properties.item(0);
142 // 的到返回值
143 String data = ((DeferredTextImpl) property).getData();
144 // Json字符串转List
145 // results = JSONArray.parseArray(data, Map.class);
146 results = (ArrayList)JsonUtil.deserialize(data);
147
148 } catch (Exception e) {
149 return null;
150 }
151 return results;
152 }
153
154 private static String doLogin(String url) throws NoSuchAlgorithmException, IOException, JSONException {
155 // String userName = FMCodeHelper.getDescByCode("USERNAME", "CLGSERVICES");//用户名
156 // String password = FMCodeHelper.getDescByCode("PASSWORD", "CLGSERVICES");//密码
157 String userName = "INTFUSER-PLCJ";//用户名
158 String password = "ab8f69e227c52086c193f0484d0227364dd7ca5c";//密码
159
160 String dataTemplate = "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">" +
161 "<SOAP-ENV:Body>%s" +
162 "</SOAP-ENV:Body>" +
163 "</SOAP-ENV:Envelope>";
164 String data = String.format(dataTemplate, "<m:doLogin xmlns:m="http://intf.services.user.sys.modules.sysmodules.scdp.csnt.com/">" +
165 "<arg0><username>" + userName + "</username>" + "<password>" + password + "</password>" + "</arg0>" + "</m:doLogin>");
166 String rlt = getloginWebserviceResponse(url, "POST", data, null);
167 int startIndex = rlt.indexOf("<return>") + 8;
168 int endIndex = rlt.indexOf("</return>", startIndex);
169 String token = rlt.substring(startIndex, endIndex);
170 System.out.println(token);
171 return token;
172 }
173
174 private static String encrySHA1(String s) throws NoSuchAlgorithmException, UnsupportedEncodingException {
175 MessageDigest sha = MessageDigest.getInstance("SHA-1");
176 sha.update(s.getBytes());
177 byte[] rlt = sha.digest();
178
179 char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
180 int j = rlt.length;
181 char[] str = new char[j * 2];
182 int k = 0;
183 for (byte byte0 : rlt) {
184 str[k++] = hexDigits[byte0 >>> 4 & 0xf];
185 str[k++] = hexDigits[byte0 & 0xf];
186 }
187
188 String rltStr = new String(str);
189 rltStr = rltStr.toLowerCase();
190
191 return rltStr;
192 }
193
194 public static String getloginWebserviceResponse(String url, String method, String data, String header) throws IOException {
195 byte[] buf = data.getBytes();
196 URL uri = new URL(url);
197 HttpURLConnection httpConn = (HttpURLConnection) uri.openConnection();
198 httpConn.setRequestMethod(method);
199 httpConn.setRequestProperty("Content-Type", "application/json");
200 httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
201 if (header != null && !"".equals(header)) {
202 httpConn.setRequestProperty("Authorization", header);
203 }
204 httpConn.setDoOutput(true);
205 httpConn.setDoInput(true);
206 OutputStream out = httpConn.getOutputStream();
207 out.write(buf);
208 out.close();
209 int code = httpConn.getResponseCode();
210 StringBuilder sb;
211 if (code == HttpURLConnection.HTTP_OK) {
212 InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), "utf-8");
213 BufferedReader br = new BufferedReader(isr);
214 sb = new StringBuilder();
215 String str;
216 while ((str = br.readLine()) != null) {
217 sb.append(str);
218 }
219 br.close();
220 isr.close();
221 } else {
222 InputStreamReader isr = new InputStreamReader(httpConn.getErrorStream(), "utf-8");
223 BufferedReader br = new BufferedReader(isr);
224 sb = new StringBuilder();
225 String str;
226 while ((str = br.readLine()) != null) {
227 sb.append(str).append("
");
228 }
229 br.close();
230 isr.close();
231 }
232 return sb.toString();
233 }
234 }
针对不对版本还有如下方法
package com.csnt.scdp.bizmodules.modules.plcj.common.helper;
import com.csnt.scdp.framework.util.JsonUtil;
import org.apache.xerces.dom.DeferredTextImpl;
import org.json.JSONException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by on 2017/5/16.
*/
public class GLSWebServiceRequest {
public static void main(String[] args) throws Exception {
Map mp= new HashMap();
mp.put("startDate","2019-04-15");
mp.put("endDate","2019-04-16");
mp.put("markName","金桥二标(城建实业)");
List result = GLSWebServiceRequest.doConnect("getYhxxConserveData", mp);
//System.out.println(a);
}
public static List doConnect(String funName, Map params) {
List<Map<String, Object>> result = new ArrayList<>();
try {
String url="http://接口ip:8099/headgmp/services/";
String dataTemplate = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pm="http://datatransfer.service.head.com">
" +
" <soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
" +
" </soapenv:Header>
" +
" <soapenv:Body>
" +
" <pm:#funName>
" +
"#param" +
" </pm:#funName>
" +
" </soapenv:Body>
" +
"</soapenv:Envelope>";
// 接口名称
dataTemplate = dataTemplate.replaceAll("#funName", funName);
StringBuffer postData = new StringBuffer();
params.forEach(((Object key, Object val) ->{
postData.append("<");
postData.append(key);
postData.append(">");
postData.append(val);
postData.append("</");
postData.append(key);
postData.append(">");
}));
// 请求参数
dataTemplate = dataTemplate.replaceAll("#param", postData.toString());
result = getWebserviceResponse(url, "POST", dataTemplate, null);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private static List getWebserviceResponse(String url, String method, String data, String header) throws
NoSuchAlgorithmException, IOException, JSONException {
//编码格式不加请求报500错误
byte[] buf = data.getBytes("UTF-8");
// 2 创建URL:
URL uri = new URL(url + "MMSDataManager?wsdl");
// 3 建立连接,并将连接强转为Http连接
HttpURLConnection httpConn = (HttpURLConnection) uri.openConnection();
httpConn.setRequestMethod(method);
// httpConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
httpConn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
httpConn.setRequestProperty("Accept", "Accept=[*/*]");
httpConn.setRequestProperty("accept-encoding", "UTF-8");
httpConn.setRequestProperty("Transfer-Encoding", "chunked");
httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
// 4,设置请求方式和请求头:
httpConn.setDoOutput(true); // 是否有入参
httpConn.setDoInput(true);// 是否有出参
httpConn.setRequestProperty("contentType", "UTF8");
httpConn.setRequestProperty("Accept-Charset", "UTF8");
OutputStream out = httpConn.getOutputStream();
out.write(buf);
out.close();
int code = httpConn.getResponseCode();
StringBuilder sb = new StringBuilder("");
if (code == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), "utf-8");
BufferedReader br = new BufferedReader(isr);
sb = new StringBuilder();
String str;
while ((str = br.readLine()) != null) {
sb.append(str);
}
br.close();
isr.close();
} else {
InputStream retStream = httpConn.getErrorStream();
if(retStream!=null){
InputStreamReader isr = new InputStreamReader(retStream, "utf-8");
BufferedReader br = new BufferedReader(isr);
sb = new StringBuilder();
String str;
while ((str = br.readLine()) != null) {
sb.append(str).append("
");
}
br.close();
isr.close();
}
}
return parserXML(sb.toString());
}
public static List parserXML(String strXML) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
List results = new ArrayList();
try {
// 解析xml
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader sr = new StringReader(strXML);
InputSource is = new InputSource(sr);
// 使用W3C的方式解析xml
Document doc = builder.parse(is);
Element rootElement = doc.getDocumentElement();
NodeList phones = rootElement.getElementsByTagName("ns:return");
Node type = phones.item(0);
NodeList properties = type.getChildNodes();
Node property = properties.item(0);
// 的到返回值
String data = ((DeferredTextImpl) property).getData();
// Json字符串转List
// results = JSONArray.parseArray(data, Map.class);
results = (ArrayList)JsonUtil.deserialize(data);
} catch (Exception e) {
return null;
}
return results;
}
public static String getloginWebserviceResponse(String url, String method, String data, String header) throws IOException {
byte[] buf = data.getBytes();
URL uri = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection) uri.openConnection();
httpConn.setRequestMethod(method);
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
if (header != null && !"".equals(header)) {
httpConn.setRequestProperty("Authorization", header);
}
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(buf);
out.close();
int code = httpConn.getResponseCode();
StringBuilder sb;
if (code == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), "utf-8");
BufferedReader br = new BufferedReader(isr);
sb = new StringBuilder();
String str;
while ((str = br.readLine()) != null) {
sb.append(str);
}
br.close();
isr.close();
} else {
InputStreamReader isr = new InputStreamReader(httpConn.getErrorStream(), "utf-8");
BufferedReader br = new BufferedReader(isr);
sb = new StringBuilder();
String str;
while ((str = br.readLine()) != null) {
sb.append(str).append("
");
}
br.close();
isr.close();
}
return sb.toString();
}
}