WebService soap报文请求与响应报文解析(JAVA)
程序员文章站
2022-06-17 10:20:11
...
参考文档:https://blog.csdn.net/qq_24505127/article/details/53057099
Json格式:
{
"TYPE": "SEALFUNC",
"METHOD": "sendSealApplicationAdm",
"SYS_TYPE": 2,
"APPLY_CODE": "20210231",
"PROJECT_NAME": "XXX用印申请",
"BRC": "001",
"BRC_NAME": "啦啦啦",
"DEPT_NAME": "啦啦啦",
"APPLY_USER": "啦啦啦",
"APPLY_USERID": "123456",
"REASON": "啦啦啦",
"CONTACT_PHONE": "15778513289",
"APPLY_DATE": "2019-12-21 10:00:00",
"SEAL_TYPE": "a",
"SEALS": [{
"NAME": "000",
"ID": "aa",
"CODE": "啦啦啦1"
}],
"USE_COUNT": 100,
"FILES": [{
"NAME": "TEST文档",
"PATH": "http://47.100.109.10:8099/Content/test/test.pdf",
"ID": "1234567891"
}]
}
直接上代码:
package com.jadyer.client;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.httpclient.HttpClient;
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;
import com.jadyer.model.GenerateSoap;
public class caService {
public static void main(String[] args) throws Exception {
GenerateSoap generateSoap=new GenerateSoap();
String json=generateSoap.getJson("SEALFUNC","sendSealApplicationAdm",2,"20210231","XXX用印申请","001","啦啦啦","啦啦啦","啦啦啦","123456","啦啦啦","15778513289",
"2019-12-21 10:00:00","a",100,"000","aa","啦啦啦1","TEST文档","http://47.100.109.10:8099/Content/test/test.pdf","1234567891");
String urlWsdl = "http://10.84.130.59:8088/Areas/Partner/webservice/StampTask.asmx?WSDL";
String jsonString=generateSoap.getSoapJson(json);
int timeout = 10000;
//方式一: HttpClient发送SOAP请求
System.out.println("HttpClient 发送SOAP请求");
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(urlWsdl);
// 设置连接超时
client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
// 设置读取时间超时
client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
// 然后把Soap请求数据添加到PostMethod中
RequestEntity requestEntity = new StringRequestEntity(jsonString, "text/xml", "UTF-8");
//设置请求头部,否则可能会报 “no SOAPAction header” 的错误
//postMethod.setRequestHeader("SOAPAction","");
// 设置请求体
postMethod.setRequestEntity(requestEntity);
//请求状态码
int status = client.executeMethod(postMethod);
// 获取响应体输入流
InputStream is = postMethod.getResponseBodyAsStream();
// 获取请求结果字符串
String result = IOUtils.toString(is);
System.out.println();
System.out.println(json);
System.out.println("status:" + status);
System.out.println("result: " + result);
// 方式二:HttpURLConnection 发送SOAP请求
System.out.println("HttpURLConnection 发送SOAP请求");
URL url = new URL(urlWsdl); HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestMethod("POST"); conn.setUseCaches(false);
conn.setDoInput(true); conn.setDoOutput(true);
conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(jsonString.getBytes("utf-8")); dos.flush();
BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream(), "utf-8")); String line = null;
StringBuffer strBuf = new StringBuffer(); while ((line = reader.readLine())
!= null) { strBuf.append(line); }
dos.close();
reader.close();
System.out.println();
System.out.println();
System.out.println(strBuf.toString());
}
}
package com.jadyer.model;
import org.junit.Test;
public class GenerateSoap {
@Test
public void test() {
String aa=getJson("SEALFUNC","sendSealApplicationAdm",2,"20210231","XXX用印申请","001","啦啦啦","啦啦啦","啦啦啦","123456","啦啦啦","15778513289",
"2019-12-21 10:00:00","a",100,"000","aa","啦啦啦1","TEST文档","http://47.100.109.10:8099/Content/test/test.pdf","1234567891");
System.out.println("json="+aa);
}
public String getJson(String type,String method,int sysType,String applyCode,String projectName,String brc,String brcName,
String deptName,String applyUser,String applyUserid,String reason,String contactPhone,String applyDate,
String sealType,int useCount,String sealsName,String sealsId,String sealsCode,String filesName,
String filesPath,String filesId) {
StringBuffer json=new StringBuffer();
json.append("{\"TYPE\": \"");
json.append(type);
json.append("\",\"METHOD\": \"");
json.append(method);
json.append("\",\"SYS_TYPE\":");
json.append(Integer.valueOf(sysType));
json.append(",\"APPLY_CODE\": \"");
json.append(applyCode);
json.append("\",\"PROJECT_NAME\": \"");
json.append(projectName);
json.append("\",\"BRC\": \"");
json.append(brc);
json.append("\",\"BRC_NAME\": \"");
json.append(brcName);
json.append("\",\"DEPT_NAME\": \"");
json.append(deptName);
json.append("\",\"APPLY_USER\": \"");
json.append(applyUser);
json.append("\",\"APPLY_USERID\": \"");
json.append(applyUserid);
json.append("\",\"REASON\": \"");
json.append(reason);
json.append("\",\"CONTACT_PHONE\": \"");
json.append(contactPhone);
json.append("\",\"APPLY_DATE\": \"");
json.append(applyDate);
json.append("\",\"SEAL_TYPE\": \"");
json.append(sealType);
json.append("\",\"SEALS\": [{\"NAME\": \"");
json.append(sealsName);
json.append("\",\"ID\": \"");
json.append(sealsId);
json.append("\",\"CODE\": \"");
json.append(sealsCode);
json.append("\"}],\"USE_COUNT\":");
json.append(Integer.valueOf(useCount));
json.append(",\"FILES\": [{\"NAME\": \"");
json.append(filesName);
json.append("\",\"PATH\": \"");
json.append(filesPath);
json.append("\",\"ID\": \"");
json.append(filesId);
json.append("\"}]}");
return json.toString();
}
public String getSoapJson(String json) {
StringBuffer sb = new StringBuffer("");
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sb.append("<soap:Envelope "
+ "xmlns:soap='http://www.w3.org/2003/05/soap-envelope' "
+ "xmlns:ws='http://ws.outinf.sealmachine.sgsg.com/'>");
sb.append("<soap:Header/>");
sb.append("<soap:Body>");
sb.append("<ws:sendSealApplicationAdm>");
sb.append("<in0>");
sb.append(json);
sb.append("</in0>");
sb.append("</ws:sendSealApplicationAdm>");
sb.append("</soap:Body>");
sb.append("</soap:Envelope>");
return sb.toString();
}
}
Json格式,根据自己实际情况拼接哈,Java的直接转为json格式,不用拼接。我这里都是大写,有点奇葩哈(不是我奇葩,某家傻逼公司提供的接口格式贼奇葩,傻逼,气死老子了)
请求报文
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://ws.outinf.sealmachine.sgsg.com/">
<soap:Header/>
<soap:Body>
<ws:sendSealApplicationAdm>
<in0>{
"TYPE": "SEALFUNC",
"METHOD": "sendSealApplicationAdm",
"SYS_TYPE": "2",
"APPLY_CODE": "20200121",
"PROJECT_NAME": "XXX用印申请",
"BRC": "001",
"BRC_NAME": "啦啦啦",
"DEPT_NAME": "啦啦啦",
"APPLY_USER": "啦啦啦",
"APPLY_USERID": "123456",
"REASON": "啦啦啦",
"CONTACT_PHONE": "15778513289",
"APPLY_DATE": "2019-12-20 10:00:00",
"SEAL_TYPE": "a",
"SEALS": [{
"NAME": "啦啦啦1",
"ID": "000",
"CODE": "aa"
}],
"USE_COUNT": 100,
"FILES": [{
"NAME": "TEST文档",
"PATH": " http://47.XXX.XXX10:8099/Content/test/test.pdf",
"ID": "1234567891"
}]
} </in0>
</ws:sendSealApplicationAdm>
</soap:Body>
</soap:Envelope>
注意: <ws:sendSealApplicationAdm> 。。。。。。</ws:sendSealApplicationAdm>,替换为自己要测试的接口方法名
如果对xml文档解析存在疑点,下面我将带大家学习XML解析方法。
扩展
对webservice soap请求,可以借助一个功能强大的软件SoapUI进行测试。也是为了防止中途错误不迷途知返。
打开界面是这样的
点击左上角的soap,会弹出弹框让你写入你的项目名(随便起)和服务地址。
如果地址正确的话,会出现这样的列表,选中一个双击点开是这样的:
然后点击左上角绿色三角会得到响应报文。
上一篇: 总结
下一篇: 理解输入系统和IMS