1.构造报文,利用soupui获取报文模板(参考附件templet.xml)。
<!--
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:upl="http://upload.neusoft.com">
<soapenv:Header/>
<soapenv:Body>
<upl:run>
<upl:json>${0}</upl:json>
</upl:run>
</soapenv:Body>
</soapenv:Envelope>
-->
2.客户端拼报文并发送请求实现:
<!--
package com.meepo.cj;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by Administrator on 2017/3/15.
*/
public class TestLocal{
public static void main(String[] args) throws Exception{
String body = "{tableName:'DA06',beginDate:'20120101',endDate:'20120101',unitcode:'32994336233070011P9391'}";
InputStream is = TestLocal.class.getResourceAsStream("templet.xml");
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(is);
TestLocal testLocal = new TestLocal();
String xml = doc.asXML().replace("${0}",body);
System.out.println(testLocal.send(xml));
}
public String send(String xml) throws Exception {
URL wsUrl = new URL("http://localhost:7000/uploadApp/entrance");
HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
OutputStream os = conn.getOutputStream();
os.write(xml.getBytes("utf-8"));
InputStream is = null;
if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
is = conn.getInputStream();
}else{
is = conn.getErrorStream();
}
byte[] b = new byte[1024];
int len = 0;
String s = "";
while((len = is.read(b)) != -1){
String ss = new String(b,0,len,"UTF-8");
s += ss;
}
// System.out.println(s);
is.close();
os.close();
conn.disconnect();
return s;
}
}
-->
备注有坑(红色部分):
1.编码转换
2.xml构造有问题的异常请求,需要通过conn.getErrorStream();获取消息错误定位问题,该问题困扰了一个下午。
如需代码方式生成报文模板,请移步:java根据wsdl生成soup报文