https://blog.csdn.net/Peng_Hong_fu/article/details/80113196

记录测试代码

SoapUI调用路径

http://localhost:8082/*/webservices/**Service?wsdl

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.xpath.DefaultXPath; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.Collections;
import java.util.List; /**
* 模拟soapUI调用WebService,解析返回报文
* Created by PengHongfu 2018-04-26 15:36
*/
public class TestSoap { //测试环境地址
public static String INVOICE_WS_URL = "http://localhost:8082/*/webservices/**Service"; public static void main(String[] args) throws Exception { String sid = "SID值";
String content = "报文内容,jOSN格式";
String tranSeq = "UUID";
String tranReqDate = "2018-04-24";
StringBuffer stringBuffer = testWebService(sid, content, tranSeq, tranReqDate); // 打印HTTP响应数据
System.out.println(stringBuffer); //处理返回数据
String xmlResult = stringBuffer.toString().replace("<", "<");
String rtnCode = getXmlMessageByName(xmlResult, "rtnCode");//报文返回状态码,0表示正常,3表示错误
String message = getXmlMessageByName(xmlResult, "message");//返回信息,主要是状态码不正常时抛出
String body = getXmlMessageByName(xmlResult, "body");//返回正文数据,需要base64解密
if ("0".equals(rtnCode)) {
//查询成功
if (StringUtils.isNotBlank(body)) {
//解密base64加密数据
Base64.Decoder decoder = Base64.getDecoder();
byte[] encodedText = body.getBytes();
String decrypt = new String(decoder.decode(encodedText), "UTF-8");
System.out.println(decrypt);
}
} else {
//查询失败
}
} // 调用WS
private static StringBuffer testWebService(String sid, String content, String tranSeq, String tranReqDate) throws Exception {
//拼接请求报文
String sendMsg = appendXmlContext(sid, content, tranSeq, tranReqDate);
// 开启HTTP连接ַ
InputStreamReader isr = null;
BufferedReader inReader = null;
StringBuffer result = null;
OutputStream outObject = null;
try {
URL url = new URL(INVOICE_WS_URL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); // 设置HTTP请求相关信息
httpConn.setRequestProperty("Content-Length",
String.valueOf(sendMsg.getBytes().length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true); // 进行HTTP请求
outObject = httpConn.getOutputStream();
outObject.write(sendMsg.getBytes()); if (200 != (httpConn.getResponseCode())) {
throw new Exception("HTTP Request is not success, Response code is " + httpConn.getResponseCode());
}
// 获取HTTP响应数据
isr = new InputStreamReader(
httpConn.getInputStream(), "utf-8");
inReader = new BufferedReader(isr);
result = new StringBuffer();
String inputLine;
while ((inputLine = inReader.readLine()) != null) {
result.append(inputLine);
}
return result; } catch (IOException e) {
throw e;
} finally {
// 关闭输入流
if (inReader != null) {
inReader.close();
}
if (isr != null) {
isr.close();
}
// 关闭输出流
if (outObject != null) {
outObject.close();
}
} } //拼接请求报文
private static String appendXmlContext(String sid, String content, String tranSeq, String tranReqDate) {
// 构建请求报文 StringBuffer stringBuffer = new StringBuffer("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:com=\"http://com.foresee.top.service/\">\n" +
" <soapenv:Body>\n" +
" <ns1:doService xmlns:ns1=\"http://cn.gov.chinatax.gt3nf.nfzcpt.service/\">\n" +
" <reqXml><![CDATA[<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<tiripPackage xmlns:xsi=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0\" xsi:type=\"tiripPackage\">\n" +
" <sessionId/>\n" +
" <service>\n" +
" <sid>" + sid + "</sid>\n" +
" <version>1.0</version>\n" +
" <tranSeq>+" + tranSeq + "</tranSeq>\n" +
" <tranReqDate>" + tranReqDate + "</tranReqDate>\n" +
" </service>\n" +
" <bizContent>\n" +
" <content>" + content + "</content>\n" +
" <paramList>\n" +
" <param>\n" +
" <name>docType</name>\n" +
" <value>json</value>\n" +
" </param>\n" +
" <param>\n" +
" <name>className</name>\n" +
" <value>GGG</value>\n" +
" </param>\n" +
" </paramList>\n" +
" </bizContent>\n" +
"</tiripPackage>\n" +
"]]></reqXml>\n" +
" </ns1:doService>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>");
return stringBuffer.toString();
} //解析报文,根据末节点名称获取值
private static String getXmlMessageByName(String xmlResult, String nodeName) throws DocumentException {
Document doc = DocumentHelper.parseText(xmlResult);
DefaultXPath xPath = new DefaultXPath("//" + nodeName);
xPath.setNamespaceURIs(Collections.singletonMap("ns1", "http://cn.gov.chinatax.gt3nf.nfzcpt.service/"));
List list = xPath.selectNodes(doc);
if (!list.isEmpty() && list.size() > 0) {
Element node = (Element) list.get(0);
return node.getText();
}
return "";
}
}

  

针对下面的报文格式,取节点值:

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:doServiceResponse xmlns:ns1="http://cn.gov.chinatax.gt3nf.nfzcpt.service/">
<return><![CDATA[<taxML><service><sid>SID值</sid><channelType>10</channelType><version>1.0</version><tranSeq>UUID</tranSeq><tranReqDate>20171204</tranReqDate></service><bizContent><bizResult><head><rtnCode>0</rtnCode><rtnMsg><code>000</code><message>处理成功</message><reason></reason></rtnMsg></head><body>PFJFU1BPTlNFX0NPT1k+(BASE64加密后的数据)</body></bizResult></bizContent><returnState><returnCode>00000</returnCode><returnMessage>Success!</returnMessage></returnState></taxML>]]></return>
</ns1:doServiceResponse>
</soap:Body>
</soap:Envelope>

  

Java 发送SOAP请求调用WebService,解析SOAP报文的更多相关文章

  1. 发送Http请求调用webService

    如果调用WebService的不是在.NET中,无法直接添加web引用,那怎么调用webservice. 有两种方式 第一种方式:GET方式 string strUrl = "http:// ...

  2. Js/Ajax中发送HttpPost请求调用WebService

    1) WebService中的方法 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(Confo ...

  3. webService 发送soap请求,并解析返回的soap报文

    本例应用场景:要做一个webService测试功能,不局限于任何一种固定格式的webService,所以像axis,cxf等框架就不好用了.只有深入到webService的原理,通过发收soap报文, ...

  4. Jmeter发送SOAP请求对WebService接口测试

    Jmeter发送SOAP请求对WebService接口测试 1.测试计划中添加一个用户自定义变量 2.HTTP信息头管理器,添加Content-Tpe,  application/soap+xml;c ...

  5. C#如何使用Soap协议调用WebService?

    WebService是什么?它的作用? WebService是一个平台独立.低耦合的.自包含的.基于可编程的可使用xml描述.调用的web应用程序,用于开发分布式的交互式的应用程序. Soap是什么? ...

  6. 使用Java发送Http请求的内容

    公司要将自己的产品封装一个WebService平台,所以最近开始学习使用Java发送Http请求的内容.这一块之前用PHP的时候写的也比较多,从用最基本的Socket和使用第三方插件都用过. 学习了J ...

  7. Java发送Http请求并获取状态码

    通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...

  8. 通过java发送http请求

    通常的http请求都是由用户点击某个连接或者按钮来发起的,但是在一些后台的Java程序中需要发送一些get或这post请求,因为不涉及前台页面,该怎么办呢? 下面为大家提供一个Java发送http请求 ...

  9. C# 使用 HttpPost 请求调用 WebService

    之前调用 WebService 都是直接添加服务引用,然后调用 WebService 方法的,最近发现还可以使用 Http 请求调用 WebService.这里还想说一句,还是 web api 的调用 ...

随机推荐

  1. AnswerOpenCV(1001-1007)一周佳作欣赏

    外国不过十一,所以利用十一假期,看看他们都在干什么. 一.小白问题 http://answers.opencv.org/question/199987/contour-single-blob-with ...

  2. topcoder srm 706 div1

    1.给定一个迷宫,点号表示不可行,井号表示可行.现在可以改变其中的一些井号的位置.问最少改变多少个井号可以使得从左上角到右下角存在路径. 思路:设高为$n$,宽为$m$,若井号的个数$S$小于$n+m ...

  3. Java——List:list.add(index, element)和list.set(index, element)的区别

    add(index, element) 含义:在集合索引为index的位置上增加一个元素element,集合list改变后list.size()会增加1 用法 testList.add(index, ...

  4. P3979 遥远的国度

    P3979 遥远的国度 思路 一开始我用这个函数得到左端点 int get_l(int x,int y) { if(top[x]==top[y]) return son[x]; int last=to ...

  5. 【ContextLoaderListener】Web项目启动报错java.lang.ClassNotFoundException: ContextLoaderListener

    错误原因: 进入到tomcat的部署路径.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\下检查了一下,发现工程部署后在WE ...

  6. ssm项目中遇到微信用户名称带有表情,插入失败问题

    ssm项目中遇到微信用户名称带有表情,插入失败问题 问题 Mysql的utf8编码最多3个字节,而Emoji表情或者某些特殊字符是4个字节. 因此会导致带有表情的昵称插入数据库时出错. 解决方法 一. ...

  7. (zhuan) Speech and Natural Language Processing

    Speech and Natural Language Processing obtain from this link: https://github.com/edobashira/speech-l ...

  8. C#winform的textbox怎么设置滚动条

    用 C#开发软件的时候文本框textbox是没有滚动条的,而且是单行文本.下面教大家如何设置多行,并且设置横向滚动条和垂直滚动条. 打开VS工具,创建一个winform窗体项目.系统会自动创建一个主窗 ...

  9. 4、Python中的类详解(0601)

    <大话数据结构>的作者程杰在博客园也有博客,网址是:http://cj723.cnblogs.com/ 面向对象编程(OOP) 1.程序 = 指令 + 数据 代码可以选择以指令为核心或以数 ...

  10. 【译】第9节---EF Code First中数据注解

    原文:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF Code-First ...