java模拟Soap请求测试Web Service接口,发现Web Service响应内容中的中文竟然是编码格式。比如:
中文:退保成功
Soap中文编码:退保成功
我仔细分析后发现,退编码实际上就是Unicode编码的Soap版,正规的Unicode编码是\u9000,Soap改成自己的格式&#x[4位内容];格式。
还有其他的比如:
换行,Soap编码:
单引号,Soap为转换为html编码:'
与号,Soap为转换为html编码:&
小于号,Soap为转换为html编码:<
等等
因此,结合以上情况。想看到直观的内容,就需要解密这些编码了。解密简单,利用正则替换实现。这里介绍下Soap中文编码的解密:
1.将退替换成\u9000。替换&#x为\u,去掉分号
2.解密Unicode编码\u9000
为了更直观看Soap响应内容,我还替换了Soap换行符编码
&#xd替换为\n换行符
java代码:
WSClient_SOAP.java

package com.sfasdfsdafd.core.webservice.test;import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.junit.Test;import com.sfasdfsdafd.core.utils.xml.XmlUtil4Jdom;/*** 类名:com.sfasdfsdafd.core.webservice.WSClient_SOAP* * <pre>* 描述: 测试类*     基本思路:*     public方法:*     特别说明:* 创建时间:2013-4-9 下午10:29:04* 修改说明: 类的修改说明* </pre>*/
public class WSClient_SOAP {static Logger log = Logger.getLogger(WSClient_SOAP.class);// 日志记录对象private static String NAMESPACE = "http://access.control.core.sfasdfsdafd.com/";private static String NAMESPACE_JBOSS = "http://access.restful.core.sfasdfsdafd.com/";private static String ENCODING_UTF_8 = "UTF-8";private static String ENCODING_GBK = "GBK";/*** 测试WebService接口 <br><pre>* @param 参数类型 参数名 说明* @return void 说明* @throws 异常类型 说明*/
//    @Testpublic void testSOAPRequest() throws IOException,Exception {long d1 = System.currentTimeMillis();String returnMsg = "";StringBuffer sTotalString = new StringBuffer();OutputStream os = null;URLConnection conn = null;InputStream is = null;try {String iDzswXml = "模拟客户端请求WebService接口";File file = new File("abc.xml");iDzswXml = FileUtils.readFileToString(file, ENCODING_GBK);iDzswXml = encodeValue(iDzswXml);StringBuffer soap = new StringBuffer();soap.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"").append(" xmlns:acc=\"" + NAMESPACE + "\"> ").append(" <soapenv:Header/>").append(" <soapenv:Body>").append(" <execService4DZSW>").append(" <iDzswXML>").append(iDzswXml).append(" </iDzswXML>").append(" </execService4DZSW>").append("</soapenv:Body>").append(" </soapenv:Envelope>");URL url = new URL("http://localhost:8080/iDzsw-interface/ws/iDzswAICWS");
//            URL url = new URL("http://10.111.184.20:13000/iDzsw-interface/ws/iDzswAICWS");conn = url.openConnection();conn.setUseCaches(false);conn.setDoInput(true);conn.setDoOutput(true);conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));conn.setRequestProperty("Content-Type", "text/xml; charset=" + ENCODING_GBK);conn.setRequestProperty("SOAPAction", "execService4DZSW");os = conn.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os, ENCODING_GBK);String reqs = soap.toString();reqs = decodeValue(reqs);log.info("模拟客户端请求WebService发送报文:\n" + reqs);osw.write(soap.toString());osw.flush();osw.close();String sCurrentLine = "";is = conn.getInputStream();BufferedReader l_reader = new BufferedReader(new InputStreamReader(is, ENCODING_GBK));while ((sCurrentLine = l_reader.readLine()) != null) {sTotalString.append(sCurrentLine);}returnMsg = sTotalString.toString();returnMsg = decodeValue(returnMsg);returnMsg = decode4SoapValue(returnMsg);} catch (Exception e) {log.error("请求WebService出现异常", e);} finally {if (os != null) {os.close();}if (is != null) {is.close();}}long d2 = System.currentTimeMillis();log.info("WebService返回:\t" + returnMsg + "\n耗时:"+ (d2 - d1));}//    @Testpublic void testSOAPRequest4JBoss() throws IOException,Exception {long d1 = System.currentTimeMillis();String returnMsg = "";StringBuffer sTotalString = new StringBuffer();OutputStream os = null;URLConnection conn = null;InputStream is = null;try {String iDzswXml = "模拟客户端请求WebService接口";File file = new File("abc.xml");iDzswXml = FileUtils.readFileToString(file, ENCODING_GBK);iDzswXml = encodeValue(iDzswXml);StringBuffer soap = new StringBuffer();soap.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"").append(" xmlns:acc=\"" + NAMESPACE_JBOSS + "\"> ").append(" <soapenv:Header/>").append(" <soapenv:Body>").append(" <acc:execService4DZSW>").append(" <iDzswXML>").append(iDzswXml).append(" </iDzswXML>").append(" </acc:execService4DZSW>").append("</soapenv:Body>").append(" </soapenv:Envelope>");URL url = new URL("http://localhost:8081/iDzsw-interface/ws/iDzswAICWS");conn = url.openConnection();conn.setUseCaches(false);conn.setDoInput(true);conn.setDoOutput(true);conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));conn.setRequestProperty("Content-Type", "text/xml; charset=" + ENCODING_GBK);conn.setRequestProperty("SOAPAction", "execService4DZSW");os = conn.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os, ENCODING_GBK);String reqs = soap.toString();reqs = decodeValue(reqs);log.info("模拟客户端请求WebService发送报文:\n" + reqs);osw.write(soap.toString());osw.flush();osw.close();String sCurrentLine = "";is = conn.getInputStream();BufferedReader l_reader = new BufferedReader(new InputStreamReader(is, ENCODING_GBK));while ((sCurrentLine = l_reader.readLine()) != null) {sTotalString.append(sCurrentLine);}returnMsg = sTotalString.toString();returnMsg = decodeValue(returnMsg);returnMsg = decode4SoapValue(returnMsg);} catch (Exception e) {log.error("请求WebService出现异常", e);} finally {if (os != null) {os.close();}if (is != null) {is.close();}}long d2 = System.currentTimeMillis();log.info("WebService返回:\t" + returnMsg + "\n耗时:"+ (d2 - d1));}/*** 测试合zuojigou的回调接口 <br><pre>* @param 参数类型 参数名 说明* @return void 说明* @throws 异常类型 说明*/
//    @Testpublic void testCallback() throws IOException,Exception {long d1 = System.currentTimeMillis();String returnMsg = "";StringBuffer sTotalString = new StringBuffer();OutputStream os = null;URLConnection conn = null;InputStream is = null;try {String iDzswXml = "URL TEST";File file = new File("abc.xml");iDzswXml = FileUtils.readFileToString(file, ENCODING_GBK);iDzswXml = encodeValue(iDzswXml);StringBuffer soap = new StringBuffer();soap.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"").append(" xmlns:acc=\"" + NAMESPACE + "\"> ").append(" <soapenv:Header/>").append(" <soapenv:Body>").append(" <callbackProcess>").append(" <xmlMsg>").append(iDzswXml).append(" </xmlMsg>").append(" </callbackProcess>").append("</soapenv:Body>").append(" </soapenv:Envelope>");URL url = new URL("http://localhost:8080/iDzsw-interface/ws/abCallbackWS");conn = url.openConnection();conn.setUseCaches(false);conn.setDoInput(true);conn.setDoOutput(true);conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));conn.setRequestProperty("Content-Type", "text/xml; charset=" + ENCODING_GBK);conn.setRequestProperty("SOAPAction", "execService4DZSW");os = conn.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os, ENCODING_GBK);log.info("模拟客户端请求WebService发送报文:\n" + soap.toString());osw.write(soap.toString());osw.flush();osw.close();String sCurrentLine = "";is = conn.getInputStream();BufferedReader l_reader = new BufferedReader(new InputStreamReader(is, ENCODING_GBK));while ((sCurrentLine = l_reader.readLine()) != null) {sTotalString.append(sCurrentLine);}returnMsg = sTotalString.toString();} catch (Exception e) {log.error("请求WebService出现异常", e);} finally {if (os != null) {os.close();}if (is != null) {is.close();}}long d2 = System.currentTimeMillis();log.info("WebService返回:\t" + returnMsg + "\n耗时:"+ (d2 - d1));}/*** 测试Socket接口 <br><pre>* @param 参数类型 参数名 说明* @return void 说明* @throws 异常类型 说明*/@Testpublic void testSocket() {// 本地地址String coreAddress = "127.0.0.1";// 测试环境地址
//        String coreAddress = "10.111.184.20";// 测试环境外网访问地址
//        String coreAddress = "221.166.48.163";// 新Project端口号int corePort = 8001;// 老Project端口号
//        int corePort = 8080;
        Socket socket = null;PrintWriter out = null;BufferedReader in = null;String message = null;// 收到的报文StringBuffer stringBuffer = new StringBuffer();String receiveString = null;File file = new File("abc.xml");try {socket = new Socket(coreAddress, corePort);out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), ENCODING_GBK), true);String msg = FileUtils.readFileToString(file, ENCODING_GBK);out.println(msg);out.flush();in = new BufferedReader(new InputStreamReader(socket.getInputStream(), ENCODING_GBK));while ((message = in.readLine()) != null) {
//                System.out.println(message);
//                System.out.println(new String(message.getBytes("UTF-8")));
                stringBuffer.append(message);}receiveString = stringBuffer.toString();} catch (UnknownHostException e) {log.error("没有找到服务器", e);} catch (IOException e) {log.error("与服务器通信出现异常", e);e.printStackTrace();} finally {try {if (out != null) {out.close();}if (in != null) {in.close();}if (socket != null) {socket.close();}} catch (IOException e) {log.error("关闭连接出现异常", e);}}log.info("客户端收到服务器返回的报文是:" + receiveString);}/*** 特殊字符转码* * @param value* @return*/private static String encodeValue(String value) {value = value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("'", "&apos;").replaceAll("\"", "&quot;");return value;}/*** 特殊字符解码* * @param value* @return*/public static String decodeValue(String value){value=value.replaceAll("&amp;", "&").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&apos;", "'").replaceAll("&quot;", "\"");return value;}/** * 解码
 &#xXXXX;* @param str * @return */public static String decode4SoapValue(String value){value=value.replaceAll("&#xd", "\n").replaceAll("&#x", "\\\\u").replaceAll(";", "");value = decodeUnicode(value);return value;}/** * 解码 Unicode \\uXXXX * @param str * @return */public static String decodeUnicode(String str) {  Charset set = Charset.forName("UTF-16");  Pattern p = Pattern.compile("\\\\u([0-9a-fA-F]{4})");  Matcher m = p.matcher( str );  int start = 0 ;  int start2 = 0 ;  StringBuffer sb = new StringBuffer();  while( m.find( start ) ) {  start2 = m.start() ;  if( start2 > start ){  String seg = str.substring(start, start2) ;  sb.append( seg );  }  String code = m.group( 1 );  int i = Integer.valueOf( code , 16 );  byte[] bb = new byte[ 4 ] ;  bb[ 0 ] = (byte) ((i >> 8) & 0xFF );  bb[ 1 ] = (byte) ( i & 0xFF ) ;  ByteBuffer b = ByteBuffer.wrap(bb);  sb.append( String.valueOf( set.decode(b) ).trim() );  start = m.end() ;  }  start2 = str.length() ;  if( start2 > start ){  String seg = str.substring(start, start2) ;  sb.append( seg );  }  return sb.toString() ;  }  }

View Code

参考文章:
http://www.dnetzj.com/Content/329.html

转载于:https://www.cnblogs.com/svennee/p/4083042.html

Web Service之Soap请求响应内容中文编码解密相关推荐

  1. Web Service和SOAP以及HTTP的关系?

    Web Service.WSDL.SOAP.HTTP的概念存在诸多联系,在不同的产品中体现的概念也不一样.本文旨在描述一下其联系 和区别. WebService 和 WSDL的关系? Web Serv ...

  2. HTTP之请求响应内容详解

    (尊重劳动成果,转载请注明出处:http://blog.csdn.NET/qq_25827845/article/details/54562339冷血之心的博客) 目录 HTTP协议(重点) 1 安装 ...

  3. SOA技术相关介绍(RPC, Web Service, REST,SOAP,JMI)

    概念介绍 SOA(面向服务的软件架构.Service Oriented Architecture),是一种软件设计模式,主要应用于不同应用组件之间通过某种协议来互操作.例如典型的  通信网络协议.因此 ...

  4. http请求响应的组成部分的介绍 用cherome查看请求响应内容 curl命令行的使用

    http请求由3部分组成:请求行 + 请求头 + 请求体 上面是一个GET,和POST请求实例 (1)请求行:由三个组成---请求HTTP的方法,URL,http版本,之间用空格分隔开 (2)请求头: ...

  5. 通过Fiddler Script替换请求/响应内容

    首先点击菜单栏中的Rules->Customize Rules(Crtl+R)打开脚本编辑器.千万千万不要直接点击Fiddler右侧的Fiddler Script,在那里面直接改是不生效的. 抛 ...

  6. F12 界面:请求响应内容 Preview 和 Response 不一致、接口返回数据和 jsp 解析到的内容不一致

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. 情况描述: 我有一个接口只是简单的查询列表数据并返回给前端作一个表格展示. 接口返回的 use ...

  7. Popular MVC框架请求响应数据加解密@Decrypt和@Encrypt的使用示例

    简介 此项目用于演示popularmvc如何提供统一全自动化的API隐私数据保护,并且可以做到业务无感和灵活指定数据加解密算法. 请求数据加密使用@Decrypt注解,响应信息加密使用@Encrypt ...

  8. SOAP最全Web Service渗透测试总结

    干货 | 最全Web Service渗透测试总结 - SecPulse.COM | 安全脉搏 0x00 前言 补充一下Web Service以及SOAP型这块资料. 0x01 Web Service基 ...

  9. web service技术之 soap

    WebService平台技术 XML+XSD,SOAP和WSDL就是构成WebService平台的三大技术. 一 : XML+XSD: WebService采用HTTP协议传输数据,采用XML格式封装 ...

最新文章

  1. Android调用WebService
  2. 点击右侧导航栏,实现iframe嵌入子页面中div,滑动到最上面
  3. powershell快捷键_借助Windows Terminal搞一个花里胡哨的PowerShell终端
  4. bootstrap3 徽章_尔冬升送张大大金像奖女神徽章,全国仅14枚,网友吐槽:他不值得...
  5. 【转】三星8552 手机提示升级系统 完成后重启 开机画面一直停留在三星的LOGO 一闪一闪 怎么办...
  6. 安装 SQL Server Compact Edition 及 与PC 数据同步的部署
  7. ES6 关于Set对象
  8. bootstrap中如何使div上下、垂直居中
  9. 新的分享之路开启,感谢您的陪伴
  10. 前端高效开发必备——常用js框架和第三方插件
  11. 腾讯微信后台开发二面凉经
  12. 没有什么秘密的学习方法
  13. Sentinel流控效果—Warm Up
  14. linux 源码编译 ./configure 的配置和用法
  15. 最新Nikon镜头序列号查询,镜头产量估算2010.1.26更新
  16. Linux 负载均衡介绍之LB介绍
  17. win8系统计算机属性在哪个文件夹,Win8文件夹选项在哪 使用Win8文件查看方式隐藏或显示文件...
  18. 2013北邮网研院上机题
  19. 在CentOS 7.6上安装MySQL 5.7.29+Navicat Premium 12 安装教程 + 注册机
  20. php浅蓝色英文,浅蓝色HTML5宽屏大气企业模板

热门文章

  1. 基于Windows 2008 R2 Core的SQL Server 2008 R2 Cluster部署(Step by Step)
  2. 马歇尔计划软件测试自学,绝密本科目考试启用前及综合应用.doc
  3. mysql的索引的区别_MYSQL索引区别
  4. linux screen 常用命令
  5. C语言程序设计第一节课作业
  6. 栈----迷宫(Maze)
  7. C++学习笔记13-类继承
  8. Tomcat报错: JDBC unregister 可能导致内存溢出
  9. 常见硬件术语大全(上)
  10. JSP实现酒店预定系统