package com.jcdm.main.webservice; import java.io.InputStream; import java.util.List; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import cn.hutool.json.XML; import com.google.gson.Gson; import com.google.gson.JsonObject; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.io.IOUtils; public class test { public static void main(String[] args) throws Exception { 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"; String content = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:rfc:functions\">\n" + " <soapenv:Header/>\n" + " <soapenv:Body>\n" + " <urn:ZPP_CF_MES_005>\n" + " <!--Optional:-->\n" + " <IV_WERKS>1000</IV_WERKS>\n" + " <!--Optional:-->\n" + " <IV_ZSCTZD>A0055577</IV_ZSCTZD>\n" + " </urn:ZPP_CF_MES_005>\n" + " </soapenv:Body>\n" + "</soapenv:Envelope>"; // HttpClientå‘é€SOAP请求 int timeout = 10000; HttpClient client = new HttpClient(); //如果需è¦ç”¨æˆ·å密ç 验è¯ï¼›ä¸éœ€è¦éªŒè¯ç™»å½•åˆ™ä¸éœ€è¦ä»¥ä¸‹4è¡Œ String username = "POMESUSER"; String password = "12345tgb"; UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(AuthScope.ANY, creds); PostMethod postMethod = new PostMethod(serviceUrl); // 设置连接超时 client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); // 设置读å–时间超时 client.getHttpConnectionManager().getParams().setSoTimeout(timeout); // 然åŽæŠŠSoap请求数æ®æ·»åŠ 到PostMethodä¸ RequestEntity requestEntity = new StringRequestEntity(content, "text/xml", "UTF-8"); // 设置请求头部,å¦åˆ™å¯èƒ½ä¼šæŠ¥ “no SOAPAction header†的错误 postMethod.setRequestHeader("SOAPAction", ""); // 设置请求体 postMethod.setRequestEntity(requestEntity); int status = client.executeMethod(postMethod); if (status == 200) {// æˆåŠŸ InputStream is = postMethod.getResponseBodyAsStream(); // 获å–请求结果å—符串 String result = IOUtils.toString(is); String jsonStr = xmlToJSON2(result); Gson gson = new Gson(); // å°†jsonå—符串转æ¢æˆå¯¹è±¡ ItemList itemList = gson.fromJson(jsonStr, ItemList.class); System.out.println("返回结果:" + result); } else { System.out.println("错误代ç :" + status + ":" + postMethod.getResponseBodyAsString()); } } /** * æ–¹å¼--è´° * 使用hutool工具包ä¸çš„工具转化 * @param xmlStr * @return */ public static String xmlToJSON2(String xmlStr){ JSONObject jsonObject1 = cn.hutool.json.XML.toJSONObject(xmlStr, true); Gson gson = new Gson(); JsonObject jsonObject2 = gson.fromJson(jsonObject1.toString(), JsonObject.class); JsonObject etData = jsonObject2 .getAsJsonObject("SOAP:Envelope") .getAsJsonObject("SOAP:Body") .getAsJsonObject("n0:ZPP_CF_MES_005.Response") .getAsJsonObject("ET_DATA"); return etData.toString(); } }