提交 | 用户 | 时间
|
0ca254
|
1 |
package com.jcdm.main.webservice; |
A |
2 |
|
|
3 |
|
|
4 |
import java.io.InputStream; |
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
import cn.hutool.json.JSONObject; |
|
8 |
import cn.hutool.json.JSONUtil; |
|
9 |
import cn.hutool.json.XML; |
|
10 |
import com.google.gson.Gson; |
|
11 |
import com.google.gson.JsonObject; |
|
12 |
import org.apache.commons.httpclient.HttpClient; |
|
13 |
import org.apache.commons.httpclient.UsernamePasswordCredentials; |
|
14 |
import org.apache.commons.httpclient.auth.AuthScope; |
|
15 |
import org.apache.commons.httpclient.methods.PostMethod; |
|
16 |
import org.apache.commons.httpclient.methods.RequestEntity; |
|
17 |
import org.apache.commons.httpclient.methods.StringRequestEntity; |
|
18 |
import org.apache.commons.io.IOUtils; |
|
19 |
|
|
20 |
|
|
21 |
public class test { |
|
22 |
|
|
23 |
public static void main(String[] args) throws Exception { |
|
24 |
String serviceUrl = "http://podqapp.cfmoto.com.cn:50200/XISOAPAdapter/MessageServlet?senderParty=&senderService=BC_MES&receiverParty=&receiverService=&interface=SI_ZPP_CF_MES_005_SYN_OUT&interfaceNamespace=http://cfmoto.com/xi/MES"; |
|
25 |
|
|
26 |
String content = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">\n" + |
|
27 |
" <soapenv:Header/>\n" + |
|
28 |
" <soapenv:Body>\n" + |
|
29 |
" <urn:ZPP_CF_MES_005>\n" + |
|
30 |
" <!--Optional:-->\n" + |
|
31 |
" <IV_WERKS>1000</IV_WERKS>\n" + |
|
32 |
" <!--Optional:-->\n" + |
|
33 |
" <IV_ZSCTZD>A0055577</IV_ZSCTZD>\n" + |
|
34 |
" </urn:ZPP_CF_MES_005>\n" + |
|
35 |
" </soapenv:Body>\n" + |
|
36 |
"</soapenv:Envelope>"; |
|
37 |
// HttpClient发送SOAP请求 |
|
38 |
int timeout = 10000; |
|
39 |
HttpClient client = new HttpClient(); |
|
40 |
//如果需要用户名密码验证;不需要验证登录则不需要以下4行 |
|
41 |
String username = "POMESUSER"; |
|
42 |
String password = "12345tgb"; |
|
43 |
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); |
|
44 |
client.getState().setCredentials(AuthScope.ANY, creds); |
|
45 |
|
|
46 |
PostMethod postMethod = new PostMethod(serviceUrl); |
|
47 |
// 设置连接超时 |
|
48 |
client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); |
|
49 |
// 设置读取时间超时 |
|
50 |
client.getHttpConnectionManager().getParams().setSoTimeout(timeout); |
|
51 |
// 然后把Soap请求数据添加到PostMethod中 |
|
52 |
RequestEntity requestEntity = new StringRequestEntity(content, "text/xml", "UTF-8"); |
|
53 |
|
|
54 |
// 设置请求头部,否则可能会报 “no SOAPAction header” 的错误 |
|
55 |
postMethod.setRequestHeader("SOAPAction", ""); |
|
56 |
// 设置请求体 |
|
57 |
postMethod.setRequestEntity(requestEntity); |
|
58 |
int status = client.executeMethod(postMethod); |
|
59 |
|
|
60 |
if (status == 200) {// 成功 |
|
61 |
InputStream is = postMethod.getResponseBodyAsStream(); |
|
62 |
// 获取请求结果字符串 |
|
63 |
String result = IOUtils.toString(is); |
|
64 |
String jsonStr = xmlToJSON2(result); |
|
65 |
Gson gson = new Gson(); |
|
66 |
// 将json字符串转换成对象 |
|
67 |
ItemList itemList = gson.fromJson(jsonStr, ItemList.class); |
|
68 |
System.out.println("返回结果:" + result); |
|
69 |
} else { |
|
70 |
System.out.println("错误代码:" + status + ":" + postMethod.getResponseBodyAsString()); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* 方式--贰 |
|
76 |
* 使用hutool工具包中的工具转化 |
|
77 |
* @param xmlStr |
|
78 |
* @return |
|
79 |
*/ |
|
80 |
public static String xmlToJSON2(String xmlStr){ |
|
81 |
JSONObject jsonObject1 = cn.hutool.json.XML.toJSONObject(xmlStr, true); |
|
82 |
|
|
83 |
Gson gson = new Gson(); |
|
84 |
JsonObject jsonObject2 = gson.fromJson(jsonObject1.toString(), JsonObject.class); |
|
85 |
JsonObject etData = jsonObject2 |
|
86 |
.getAsJsonObject("SOAP:Envelope") |
|
87 |
.getAsJsonObject("SOAP:Body") |
|
88 |
.getAsJsonObject("n0:ZPP_CF_MES_005.Response") |
|
89 |
.getAsJsonObject("ET_DATA"); |
|
90 |
return etData.toString(); |
|
91 |
} |
|
92 |
|
|
93 |
} |