WebService接口

WebService接口这是我在做对外部系统推送数据时自己写的WebService推送接口工具类,有几点需要注意1、我们调用对方的WebService接口,对方会给一个WebService接口的地址,供我们访问:http:

大家好,又见面了,我是你们的朋友全栈君。

 

这是我在做对外部系统推送数据时自己写的WebService推送接口工具类,有几点需要注意

1、我们调用对方的WebService接口,对方会给一个WebService接口的地址,供我们访问:http://localhost:8080/cgs-ui/services/NC_TFM_INF_FundService?wsdl

直接访问这个地址之后我们看到对方接口中可以供调用的方法名,如下:

<span role="heading" aria-level="2">WebService接口

 

 2、具体的调用都在下方代码中,代码中的一些参数,都有注释

  1 package com.ritoinfo.tf2m.arapPayment.util;
  2 
  3 import java.io.ByteArrayInputStream;
  4 import java.io.IOException;
  5 import java.io.InputStream;
  6 import java.util.HashMap;
  7 import java.util.Map;
  8 
  9 import org.apache.commons.httpclient.HttpClient;
 10 import org.apache.commons.httpclient.HttpException;
 11 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
 12 import org.apache.commons.httpclient.methods.PostMethod;
 13 import org.apache.commons.httpclient.methods.RequestEntity;
 14 import org.apache.commons.httpclient.params.HttpMethodParams;
 15 import org.apache.commons.logging.Log;
 16 import org.apache.commons.logging.LogFactory;
 17 
 18 import com.longtop.tfm.bas.util.UtilString;
 19 
 20 /**
 21 * @Description  <br/>
 22 * @author 
 23 * @version 
 24 */
 25 public class WebServiceUtil {
 26     
 27     public static final Log log = LogFactory.getLog(WebServiceUtil.class);
 28     
 29     /**
 30      * @Title: send
 31      * @Description: 发送请求
 32      * @param @param map
 33      * @param @return 参数
 34      * @return Map 返回类型
 35      * @throws
 36      */
 37     public static Map send(Map map){
 38         
 39         //组装报文
 40         String sendXml = getSoapMsg(map);
 41         log.error("发送出去的报文:" + sendXml);
 42         
 43         // 创建httpClient实例对象
 44         HttpClient httpClient = new HttpClient();
 45         // 设置httpClient连接主机服务器超时时间:15000毫秒
 46         httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
 47         // 创建post请求方法实例对象
 48         PostMethod postMethod = new PostMethod((String) map.get("path"));
 49         // 设置post请求超时时间
 50         postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
 51         // 在头文件中设置转码
 52         postMethod.addRequestHeader("Content-Type","text/xml;charset=UTF-8");
 53         Map returnMap = new HashMap<Object, Object>();
 54         try {
 55             //然后把Soap请求数据添加到PostMethod中
 56             byte[] b = null;
 57             b = sendXml.getBytes("utf-8");
 58             InputStream is = new ByteArrayInputStream(b, 0, b.length);
 59             RequestEntity re = new InputStreamRequestEntity(is, b.length);
 60             postMethod.setRequestEntity(re);
 61             // 发送请求
 62             httpClient.executeMethod(postMethod);
 63             // 返回的结果
 64             String result = postMethod.getResponseBodyAsString();
 65             map.put("result", result);
 66             // 将返回报文中的转义字符进行转换
 67             returnMap = analysisXml(map);
 68         } catch (HttpException e) {
 69             log.error("发送HTPP请求报错!!!");
 70             e.printStackTrace();
 71         } catch (IOException e) {
 72             log.error("发送HTPP请求报错!!!");
 73             e.printStackTrace();
 74         }
 75         postMethod.releaseConnection();
 76 
 77         return returnMap;
 78     }
 79     
 80     /**
 81      * @Title: getSoapMsg
 82      * @Description: 组装发送的soapUI报文
 83      * 具体的WebService的发送的报文需要接收放提供模板,然后将下面的报文进行改进
 84      * methodName是我们调用对方WebService接口需要调用的方法名,在对方的wsdl文件中也会有体现的
 85      * @param @param map
 86      * @param @return 参数
 87      * @return String 返回类型
 88      * @throws
 89      */
 90     private static String getSoapMsg(Map map) {
 91         StringBuffer sb = new StringBuffer();
 92         sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"").append("http://service.webservice.core.data.ridol.ritoinfo.com/").append("\">");
 93         sb.append("<soapenv:Header/>");
 94         sb.append("<soapenv:Body>");
 95         sb.append("<ser:").append(map.get("methodName")).append(">");
 96         sb.append("<xml>");
 97         sb.append("<![CDATA[");
 98         // =========要发送的报文===============
 99         sb.append(map.get("xml"));
100         // =================================
101         sb.append("]]>");
102         sb.append("</xml>");
103         sb.append("</ser:").append(map.get("methodName")).append(">");
104         sb.append("</soapenv:Body>");
105         sb.append("</soapenv:Envelope>");
106         return sb.toString();
107     }
108     
109     /**
110      * @Title: analysisXml
111      * @Description: 转义特殊字符并将结果输出
112      * @param @param map
113      * @param @return 参数
114      * @return Map 返回类型
115      * @throws
116      */
117     private static Map analysisXml(Map map){
118         Map returnMap = new HashMap<Object, Object>();
119         String result = (String) map.get("result");
120         if(UtilString.isNotEmpty(result)){
121             result = result.replaceAll("&lt;","<"); 
122             result = result.replaceAll("&gt;",">"); 
123             result = result.replaceAll("&apos;","'"); 
124             result = result.replaceAll("&quot;","\""); 
125             result = result.replaceAll("&#xD;","\r"); 
126             result = result.replaceAll("&amp;"," "); 
127             result = result.replaceAll("&#x9;"," ");
128             
129             int statusBegin = result.indexOf("<status>");
130             int statusEnd = result.indexOf("</status>");
131             String status = result.substring((statusBegin + "<status>".length()),statusEnd);
132             
133             int messageBegin = result.indexOf("<message>");
134             int messageEnd = result.indexOf("</message>");
135             String message = result.substring((messageBegin + "<message>".length()),messageEnd);
136             returnMap.put("status", status);
137             returnMap.put("message", message);
138             returnMap.put("result", result);
139         }
140         return returnMap;
141     }
142 }

 

package com.ritoinfo.tf2m.arapPayment.util;
import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.Map;
import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.methods.InputStreamRequestEntity;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.RequestEntity;import org.apache.commons.httpclient.params.HttpMethodParams;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
import com.longtop.tfm.bas.util.UtilString;
/*** @Description  <br/>* @author * @version */public class WebServiceUtil {public static final Log log = LogFactory.getLog(WebServiceUtil.class);/** * @Title: send * @Description: 发送请求 * @param @param map * @param @return 参数 * @return Map 返回类型 * @throws */public static Map send(Map map){//组装报文String sendXml = getSoapMsg(map);log.error(“发送出去的报文:” + sendXml);// 创建httpClient实例对象        HttpClient httpClient = new HttpClient();        // 设置httpClient连接主机服务器超时时间:15000毫秒        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);        // 创建post请求方法实例对象        PostMethod postMethod = new PostMethod((String) map.get(“path”));        // 设置post请求超时时间        postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);// 在头文件中设置转码        postMethod.addRequestHeader(“Content-Type”,”text/xml;charset=UTF-8″);        Map returnMap = new HashMap<Object, Object>();        try {        //然后把Soap请求数据添加到PostMethod中    byte[] b = null;    b = sendXml.getBytes(“utf-8”);    InputStream is = new ByteArrayInputStream(b, 0, b.length);    RequestEntity re = new InputStreamRequestEntity(is, b.length);    postMethod.setRequestEntity(re);    // 发送请求httpClient.executeMethod(postMethod);// 返回的结果String result = postMethod.getResponseBodyAsString();map.put(“result”, result);// 将返回报文中的转义字符进行转换returnMap = analysisXml(map);} catch (HttpException e) {log.error(“发送HTPP请求报错!!!”);e.printStackTrace();} catch (IOException e) {log.error(“发送HTPP请求报错!!!”);e.printStackTrace();}        postMethod.releaseConnection();
return returnMap;}/** * @Title: getSoapMsg * @Description: 组装发送的soapUI报文 * 具体的WebService的发送的报文需要接收放提供模板,然后将下面的报文进行改进 * methodName是我们调用对方WebService接口需要调用的方法名,在对方的wsdl文件中也会有体现的 * @param @param map * @param @return 参数 * @return String 返回类型 * @throws */private static String getSoapMsg(Map map) {StringBuffer sb = new StringBuffer();sb.append(“<soapenv:Envelope xmlns:soapenv=\”http://schemas.xmlsoap.org/soap/envelope/\” xmlns:ser=\””).append(“http://service.webservice.core.data.ridol.ritoinfo.com/”).append(“\”>”);sb.append(“<soapenv:Header/>”);sb.append(“<soapenv:Body>”);sb.append(“<ser:”).append(map.get(“methodName”)).append(“>”);sb.append(“<xml>”);sb.append(“<![CDATA[“);// =========要发送的报文===============sb.append(map.get(“xml”));// =================================sb.append(“]]>”);sb.append(“</xml>”);sb.append(“</ser:”).append(map.get(“methodName”)).append(“>”);sb.append(“</soapenv:Body>”);sb.append(“</soapenv:Envelope>”);return sb.toString();}/** * @Title: analysisXml * @Description: 转义特殊字符并将结果输出 * @param @param map * @param @return 参数 * @return Map 返回类型 * @throws */private static Map analysisXml(Map map){Map returnMap = new HashMap<Object, Object>();String result = (String) map.get(“result”);if(UtilString.isNotEmpty(result)){result = result.replaceAll(“&lt;”,”<“); result = result.replaceAll(“&gt;”,”>”); result = result.replaceAll(“&apos;”,”‘”); result = result.replaceAll(“&quot;”,”\””); result = result.replaceAll(“&#xD;”,”\r”); result = result.replaceAll(“&amp;”,” “); result = result.replaceAll(“&#x9;”,” “);int statusBegin = result.indexOf(“<status>”);    int statusEnd = result.indexOf(“</status>”);    String status = result.substring((statusBegin + “<status>”.length()),statusEnd);        int messageBegin = result.indexOf(“<message>”);    int messageEnd = result.indexOf(“</message>”);    String message = result.substring((messageBegin + “<message>”.length()),messageEnd);    returnMap.put(“status”, status);    returnMap.put(“message”, message);    returnMap.put(“result”, result);}return returnMap;}}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154740.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【Custom Mutator Fuzz】Libprotobuf + LibFuzzer联合使用

    【Custom Mutator Fuzz】Libprotobuf + LibFuzzer联合使用终于到了与fuzzer结合使用的章节了,本篇文章为Libprotobufmutatorfuzzinglearning项目的第二个练习,其中有一些坑点,在本文中也进行了标注编写不易,如果能够帮助到你,希望能够点赞收藏加关注哦Thanks♪(・ω・)ノPS:文章末尾有联系方式,交个朋友吧~本文链接:模糊测试系列往期回顾:【CustomMutatorFuzz】简单Protobuf使用练习【CustomMutatorFuzz】ProtocolBuffer基础(下):C++生成代.

    2022年9月13日
    4
  • 如何修改host文件[通俗易懂]

    如何修改host文件[通俗易懂]一.host是什么:是一个没有扩展名的系统文件,可以用记事本等工具打开二.为何要修改host:1).就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中寻找对应的IP地址,一旦找到,系统会立即打开对应网页,如果没有找到,则系统会再将网址提交DNS域名解析服务器进行IP地址的解析。**2).加快域名解析** 对于要经常访问的网站,我们可以通过在Hosts中配置域名和IP的映射关系,提高域名解析速度。由于有了

    2022年10月12日
    2
  • 全012路规律_11选5判断012路的方法

    全012路规律_11选5判断012路的方法堆题目链接将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:x is the root:x是根结点;x and y are siblings:x和y是兄弟结点;x is the parent of y:x是y的父结点;x is a child of y:x是y的一个子结点。输入格式:每组测试第1行包含2个正整数N(≤ 1000)和M(≤ 20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[−10000,10000]内的N个要被

    2022年8月8日
    6
  • RegisterStartupScript 使用误区

    RegisterStartupScript 使用误区在【孟子E章】专栏里,我曾经发表了一篇《ASP.NET2.0中CSS失效的问题总结》(地址是:http://blog.csdn.net/net_lover/archive/2007/08/27/1760521.aspx)的文章。有些人不知道使用RegisterStartupScript输出文本(非脚本内容),只能使用Response.Write的方法。这其实是对RegisterStartupSc…

    2022年7月20日
    17
  • matlab如何批量读取图片_nu(n)*nu(n)卷积

    matlab如何批量读取图片_nu(n)*nu(n)卷积有一张RGB的图像,我们要在这个图像的周围加上填充元素,使得这个图像不会再卷积操作后导致边缘信息丢失和图像尺寸的减小。为此,我们需要padding操作,numpy库中对这个进行了封装numpy.pad()函数:对一个一维数组来说:但是我们的图像至少是二维的(灰度图),我们要在这样的格式下进行填充,就需要理解到图像在空间位置上的脑补图:在参数传递中,我们只需要计算…

    2022年8月13日
    5
  • linux杀死进程命令kill_linux彻底杀死进程

    linux杀死进程命令kill_linux彻底杀死进程kill-9进程名杀死进程

    2022年9月29日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号