# webservice-utils **Repository Path**: zhang-yiya/webservice-utils ## Basic Information - **Project Name**: webservice-utils - **Description**: WebService调用,超简单 - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 2 - **Created**: 2023-07-20 - **Last Updated**: 2025-07-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: webservice ## README # 如何使用http请求来调用webService服务? **最近一直在使用webService服务,其中使用的工具一直是axis2,但是我感觉太麻烦了,在了解完webService服务后我发现其实这玩意就是个post请求加上了soap协议,然后我就自己封装了一个小工具,自己用着还是蛮方便的,感觉功能上还能满足我的日常工作,嘿嘿今天分享出来欢迎大家使用,有什么封装的不好或者不对的地方欢迎大家指正** ### 可配置项 1. 配置调用超时时间:WebServiceHttpConnect connect=new WebServiceHttpConnect(超时时长); > [!IMPORTANT] > > 注:超时时长单位为毫秒,默认不传为10000ms 2. 配置请求头:connect.addRequestHeader("head1","xxx"); > [!IMPORTANT] > > 注:soap1.2的请求方式默认不带SOAPAction请求头,若有需要可自定义SOAPAction > > connect.addRequestHeader("SOAPAction","xxxxxxxx"); ### 调用方法说明 ##### 1.调用soap1.1接口 > [!NOTE] > > 如何查找方法的SOAPAction? > > 打开wsdl文件找到你想要调用的方法名例如: > > ![输入图片说明](fc5fd60bc85e4a0caf19e4555c91076e.jpeg) - 普通调用非https加密的调用: **soap11HttpSend(String urlStr,String soapXml,String SOAPAction)** - https请求跳过ssl认证: **soap11HttpsSend(String urlStr,String soapXml,String SOAPAction)** ##### 2.调用soap1.2接口 - 普通调用非https加密的调用: **soap12HttpSend(String urlStr,String soapXml,String SOAPAction)** - https请求跳过ssl认证: **soap12HttpsSend(String urlStr,String soapXml,String SOAPAction)** ##### 3.WebServiceUtils方法:截取指定标签内的内容 ```java String body = WebServiceUtils.cutOutXml("标签名", "需要截取的xml"); ``` ### 使用流程 - 步骤一:将依赖拉取到自己的pom文件中maven地址为[Maven Central: io.gitee.zhang-yiya:webservice-utils:3.0.0 (sonatype.com)](https://central.sonatype.com/artifact/io.gitee.zhang-yiya/webservice-utils/3.0.0/overview) ```xml io.gitee.zhang-yiya webservice-utils 3.0.0 ``` - 步骤二:创建一个WebServiceHttpConnect对象 ```java WebServiceHttpConnect connect=new WebServiceHttpConnect(); //或5000为请求超时时间单位为ms,默认不填为10000ms WebServiceHttpConnect connect=new WebServiceHttpConnect(5000); ``` - 步骤三:向服务端发送请求 ```java ResponseExample responseExample = connect.soap12HttpSend("请求地址", "包含soap协议的xml"); ``` - 步骤四:获取状态码和响应内容 ```java Integer responseCode = responseExample.getResponseCode(); //状态码 String responseContext = responseExample.getResponseContext(); //相应内容 ``` ### 调用示例 ```java WebServiceHttpConnect connect=new WebServiceHttpConnect(); String url="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"; String soapXml="\n" + " \n" + " \n" + " \n" + " \n" + ""; ResponseExample responseExample = connect.soap12HttpSend(url, soapXml); //请求 Integer responseCode = responseExample.getResponseCode(); //状态码 String responseContext = responseExample.getResponseContext(); //相应内容 ```