webservice的调用方式

合集下载

SoapUI调用webservice实现的两种方式

SoapUI调用webservice实现的两种方式

SoapUI调⽤webservice实现的两种⽅式SoapUI⽤来做测试,已经是很多⼈做过的事情了,⽽且⽹上教程也很多。

不过还是写下来,对⽐webservice协议与http协议脚本编写的不同。

⾸先测接⼝得有⼀个服务,刚好笔者所在项⽬做ESB集成。

就顺道⽤⼀下⼯作环境截⼏张图。

业务模型: 从客户端post⼀个字符串给ESB服务器,ESB服务器收到该请求后,返回⼀个在原字符串前添加⼀个Hello。

请求的报⽂添加三个head“Svc_Inbound","TEST"”Svc_ServiceCode","AAA-TEST-BBB""Svc_Version","1"报⽂正⽂<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="/soap/envelope/" xmlns:ser=""><soapenv:Header/><soapenv:Body><ser:sayHi><ser:in0>this is a request</ser:in0></ser:sayHi></soapenv:Body></soapenv:Envelope>服务器返回的报⽂<soap:Envelope xmlns:soap="/soap/envelope/" xmlns:xsd="/2001/XMLSchema" xmlns:xsi="/2001/XMLSchema-instance"> <soap:Body><ns1:sayHiResponse xmlns:ns1=""><ns1:out>Hello this is a request</ns1:out></ns1:sayHiResponse></soap:Body></soap:Envelope>1.通过SOAP协议实现功能的实现,⾸先⽤soapui来实现,打开soapui ,file---new soap project在⼯程创建弹出框输⼊WSDL地址,输⼊⼯程名称,点击OK双击Request 1,在创建⼯程时创建的默认请求,双击后会看到该请求内容。

java 调用.net webservice的方法

java 调用.net webservice的方法

java 调用.net webservice的方法在Java和.NET之间进行交互通常涉及到两种不同编程语言和平台之间的通信。

其中,.NET Web服务是一种常用的跨平台通信方式。

在Java中调用.NET Web 服务,可以通过多种方式实现,包括使用HTTP客户端库、SOAP库和Web服务适配器等。

下面将介绍一种常见的方法,以帮助您在Java中调用.NET Web服务。

一、准备工作1.确保您的Java应用程序已经安装并配置好。

2.确保您的.NET Web服务已经发布并可用。

3.安装适当的Java HTTP客户端库或SOAP库,以便在Java中调用Web服务。

二、Java调用.NET Web服务的步骤1.创建HTTP客户端对象使用Java HTTP客户端库(如Apache HttpClient或Java的HttpURLConnection)创建一个HTTP客户端对象,用于与.NET Web服务进行通信。

```javaHttpClient httpClient = HttpClientBuilder.create().build();```2.构建SOAP请求消息使用适当的SOAP库(如JAX-WS或CXF)构建SOAP请求消息,并将其发送到.NET Web服务。

确保您的SOAP请求符合Web服务的文档描述。

```javaSOAPMessage soapRequest = MessageFactory.createMessage();soapRequest.getSOAPPart().writeTo(System.out);```3.发送SOAP请求使用HTTP客户端对象将SOAP请求消息发送到.NET Web服务。

您可以使用HttpClient对象的execute()方法执行HTTP请求。

```javaHttpResponse httpResponse = httpClient.execute(webServiceUrl, context);```4.接收SOAP响应消息从Web服务的响应中获取SOAP响应消息,并解析其内容。

mfc程序中调用webservice的实现

mfc程序中调用webservice的实现

MFC程序中调用webservice的实现介绍在MFC程序中调用webservice是一种常见的需求,通过调用webservice可以实现与远程服务器的数据交互。

本文将详细介绍如何在MFC程序中调用webservice的实现方法。

准备工作在开始之前,首先需要准备以下内容: 1. Visual Studio:在Windows平台上开发MFC程序常用的集成开发环境。

2. webservice接口:需要事先获取webservice的接口地址和相关参数。

3. MFC项目:创建一个MFC项目作为开发的基础。

步骤一:导入webservice引用1.打开MFC项目,在资源视图中右键单击名为”References”的文件夹,选择”Add Service Reference”。

2.在弹出的对话框中,输入webservice的地址,点击“Go”按钮。

3.Visual Studio将会检测webservice,并显示相关的方法和数据类型。

点击“OK”按钮确认导入。

步骤二:调用webservice方法1.在MFC项目中找到需要调用webservice的代码位置。

2.在代码中添加相关的头文件和命名空间:#include "yourWebService.h"using namespace YourWebServiceNamespace;3.创建webservice的实例并调用相应的方法:YourWebServiceName^ service = gcnew YourWebServiceName();service->YourMethod(param1, param2, ...);步骤三:处理webservice返回结果1.在调用webservice的方法之后,通常会返回一些数据信息。

2.根据webservice的返回类型,可以使用相应的数据类型接收返回值,例如字符串、整数、对象等。

3.根据业务需求,可以对返回结果进行处理,例如显示、存储或者进一步处理等。

vb6 调用webservice 参数

vb6 调用webservice 参数

vb6 调用webservice 参数
在 Visual Basic 6.0 中调用 Web Service 参数时,如果参数的字符串长度超过 400 个字符,可能会遇到一些问题。

这是因为 Visual Basic 6.0 使用的是 COM 技术,而 COM 对字符串的长度有一些限制。

要处理超过 400 个字符的 Web Service 参数,可以考虑以下几种方法:
1. 使用字节数组:将长字符串作为字节数组传递给 Web Service。

在 Visual Basic 6.0 中,可以使用 `Byte()` 函数将字符串转换为字节数组。

这样可以避免字符串长度的限制。

2. 截断字符串:如果不需要完整传递超过 400 个字符的字符串,可以在 Visual Basic 6.0 中将其截断为较短的字符串。

然后,在 Web Service 端进行处理时,可以根据需要获取完整的字符串。

3. 升级到更高版本的开发环境:考虑将项目迁移到支持更长字符串的更高版本的开发环境,如 Visual Basic .NET 或其他现代的开发工具。

4. 使用其他方法传递参数:如果字符串长度超过 400 个字符是常态,可以考虑使用其他方式传递参数,如文件上传、XML 数据等。

需要根据具体情况选择适合的方法。

如果可能的话,尽量避免使用过长的字符串作为Web Service 参数,以提高应用程序的性能和可维护性。

webservice接口调用示例

webservice接口调用示例

webservice接口调用示例WebService接口是一个用于数据交互的网络服务,其接口通常使用HTTP或HTTPS方式调用。

以查询天气信息为例,下面是一个使用WebService接口调用的示例:1. 首先,我们需要获取天气查询接口的地址和参数。

这些信息通常可以在接口提供方的文档中找到。

2. 我们可以使用类似cURL、Python的requests库等工具来发送HTTP请求。

以requests库为例,我们可以在代码中添加如下语句导入requests库:```import requests```3. 接下来,我们需要发送HTTP GET请求获取天气信息。

代码示例如下:```url = '/currentconditions/v1/'payload = {'apikey': 'YOUR_API_KEY', 'location': 'beijing'} response = requests.get(url, params=payload)```4. 上述代码中,我们指定了接口的地址`url`和查询参数`payload`。

此外,我们还需要向接口提供方获取一个API Key,以便进行认证。

5. 接下来,我们可以通过解析服务端响应中的JSON数据获取天气信息。

代码示例如下:```data = response.json()weather_info = data[0]['WeatherText']temperature = data[0]['Temperature']['Metric']['Value']```6. 上述代码中,我们将服务端响应中的JSON数据解析,并获取其中的天气信息和温度信息。

7. 最后,我们可以将结果展示给用户或者进行其他处理。

```result = '北京的天气为{},温度为{}度'.format(weather_info, temperature)print(result)```请注意,上述代码仅为示例,实际的WebService接口调用可能需要进行更多的参数配置和错误处理。

webservice soap调用

webservice soap调用

webservice soap调用Webservice是一种基于web的远程通信技术,它通过标准的XML 消息进行通信,可以跨越平台和语言的限制,实现不同系统之间的互联互通。

SOAP是其中一种实现方式,它使用了XML格式的消息来描述操作和数据,通过HTTP或其他协议进行网络传输。

本文将介绍如何使用SOAP调用webservice服务。

SOAP调用webservice需要依照以下步骤进行:1. 找到webservice服务的WSDL文件,这个文件描述了服务的接口和操作。

2. 使用SOAP工具生成客户端代码,如Java中的wsimport命令。

这个步骤会自动生成可以调用服务的Java类文件。

3. 在Java代码中使用生成的类文件实例化服务端口,并为要调用的操作设置参数。

4. 调用服务端口的相应操作,获取返回结果。

下面我们来详细讲解一下这些步骤。

首先,在webservice服务提供方的网站中找到WSDL文件链接。

打开WSDL文件后,查看其中的接口和操作,了解服务提供方的数据类型和函数。

然后,在本地使用wsimport命令生成客户端代码。

在命令行终端中输入:wsimport -keep [WSDL文件链接]命令执行完毕后,会生成很多Java类文件,这些类可以直接被我们调用。

接着,我们在Java代码中调用生成的客户端类。

先实例化服务端口,再为要调用的操作设置参数。

例如,如果我们要调用一个返回天气的接口,代码如下:WeatherServicePortType weatherService = new WeatherService().getWeatherServiceHttpPort();String city = "Beijing";String weather = weatherService.getWeather(city);最后,调用服务端口的操作并获取返回结果。

在上面的代码中,我们调用了WeatherService服务的getWeather方法,并传入了一个城市参数“Beijing”,获取对应的天气信息。

WebService接口的几种调用方式--wsdl文件类型

WebService接口的几种调用方式--wsdl文件类型

WebService接⼝的⼏种调⽤⽅式--wsdl⽂件类型⾸先⽐如查询到的⼀个wsdl⽂件内容如下:<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions targetNamespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" xmlns:apachesoap="/xml-soap" xmlns:impl="http://198.168.0.88:6888/ormrpc/services/EASLogin" xmlns:intf="http://198.168.0.88:68 <!--WSDL created by Apache Axis version: 1.4Built on Apr 22, 2006 (06:55:48 PDT)--><wsdl:types><schema targetNamespace="urn:client" xmlns="/2001/XMLSchema"><import namespace="/soap/encoding/"/><complexType name="WSContext"><sequence><element name="dbType" type="xsd:int"/><element name="dcName" nillable="true" type="xsd:string"/><element name="password" nillable="true" type="xsd:string"/><element name="sessionId" nillable="true" type="xsd:string"/><element name="slnName" nillable="true" type="xsd:string"/><element name="userName" nillable="true" type="xsd:string"/></sequence></complexType></schema></wsdl:types><wsdl:message name="logoutResponse"><wsdl:part name="logoutReturn" type="xsd:boolean"></wsdl:part></wsdl:message><wsdl:message name="loginResponse"><wsdl:part name="loginReturn" type="tns1:WSContext"></wsdl:part></wsdl:message><wsdl:message name="loginRequest"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="password" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part><wsdl:part name="dbType" type="xsd:int"></wsdl:part><wsdl:part name="authPattern" type="xsd:string"></wsdl:part></wsdl:message><wsdl:message name="loginResponse1"><wsdl:part name="loginReturn" type="tns1:WSContext"></wsdl:part></wsdl:message><wsdl:message name="logoutRequest"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part></wsdl:message><wsdl:message name="loginResponse2"><wsdl:part name="loginReturn" type="tns1:WSContext"></wsdl:part></wsdl:message><wsdl:message name="loginRequest1"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="password" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part><wsdl:part name="dbType" type="xsd:int"></wsdl:part></wsdl:message><wsdl:message name="loginByLtpaTokenRequest"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="ltpaToken" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part><wsdl:part name="dbType" type="xsd:int"></wsdl:part></wsdl:message><wsdl:message name="loginRequest2"><wsdl:part name="userName" type="xsd:string"></wsdl:part><wsdl:part name="password" type="xsd:string"></wsdl:part><wsdl:part name="slnName" type="xsd:string"></wsdl:part><wsdl:part name="dcName" type="xsd:string"></wsdl:part><wsdl:part name="language" type="xsd:string"></wsdl:part><wsdl:part name="dbType" type="xsd:int"></wsdl:part><wsdl:part name="authPattern" type="xsd:string"></wsdl:part><wsdl:part name="isEncodePwd" type="xsd:int"></wsdl:part></wsdl:message><wsdl:message name="loginByLtpaTokenResponse"><wsdl:part name="loginByLtpaTokenReturn" type="tns1:WSContext"></wsdl:part></wsdl:message><wsdl:portType name="EASLoginProxy"><wsdl:operation name="logout" parameterOrder="userName slnName dcName language"><wsdl:input message="impl:logoutRequest" name="logoutRequest"></wsdl:input><wsdl:output message="impl:logoutResponse" name="logoutResponse"></wsdl:output></wsdl:operation><wsdl:operation name="login" parameterOrder="userName password slnName dcName language dbType authPattern"> <wsdl:input message="impl:loginRequest" name="loginRequest"></wsdl:input><wsdl:output message="impl:loginResponse" name="loginResponse"></wsdl:output></wsdl:operation><wsdl:operation name="login" parameterOrder="userName password slnName dcName language dbType"><wsdl:input message="impl:loginRequest1" name="loginRequest1"></wsdl:input><wsdl:output message="impl:loginResponse1" name="loginResponse1"></wsdl:output></wsdl:operation><wsdl:operation name="login" parameterOrder="userName password slnName dcName language dbType authPattern isEncodePwd"><wsdl:input message="impl:loginRequest2" name="loginRequest2"></wsdl:input><wsdl:output message="impl:loginResponse2" name="loginResponse2"></wsdl:output></wsdl:operation><wsdl:operation name="loginByLtpaToken" parameterOrder="userName ltpaToken slnName dcName language dbType"><wsdl:input message="impl:loginByLtpaTokenRequest" name="loginByLtpaTokenRequest"></wsdl:input><wsdl:output message="impl:loginByLtpaTokenResponse" name="loginByLtpaTokenResponse"></wsdl:output></wsdl:operation></wsdl:portType><wsdl:binding name="EASLoginSoapBinding" type="impl:EASLoginProxy"><wsdlsoap:binding style="rpc" transport="/soap/http"/><wsdl:operation name="logout"><wsdlsoap:operation soapAction=""/><wsdl:input name="logoutRequest"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="logoutResponse"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation><wsdl:operation name="login"><wsdlsoap:operation soapAction=""/><wsdl:input name="loginRequest"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="loginResponse"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation><wsdl:operation name="login"><wsdlsoap:operation soapAction=""/><wsdl:input name="loginRequest1"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="loginResponse1"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation><wsdl:operation name="login"><wsdlsoap:operation soapAction=""/><wsdl:input name="loginRequest2"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="loginResponse2"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation><wsdl:operation name="loginByLtpaToken"><wsdlsoap:operation soapAction=""/><wsdl:input name="loginByLtpaTokenRequest"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="" use="encoded"/> </wsdl:input><wsdl:output name="loginByLtpaTokenResponse"><wsdlsoap:body encodingStyle="/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output></wsdl:operation></wsdl:binding><wsdl:service name="EASLoginProxyService"><wsdl:port binding="impl:EASLoginSoapBinding" name="EASLogin"><wsdlsoap:address location="http://198.168.0.88:6888/ormrpc/services/EASLogin"/></wsdl:port></wsdl:service></wsdl:definitions>⼀般有⼏种⽅式调⽤此接⼝:⽅式⼀:通过wsdl⽂件或者链接使⽤相关IDE软件⽣成Java⽂件这种⽅式⽐较累赘,不做推荐,使⽤IDE软甲即可完成⽅式⼆:使⽤apache的动态代理⽅式实现话不多说,直接上代码:import .URL;import space.QName;import org.apache.axis.client.Call;import org.apache.axis.client.Service;public class UsingDII {public static void main(String[] args) {try {int a = 100, b=60; // 对应的targetNamespaceString endPoint = "http://198.168.0.88:6888/ormrpc/services/EASLogin";Service service = new Service();Call call = (Call)service.createCall();call.setOperationName(new QName(endPoint,"EASLogin"));call.setTargetEndpointAddress(new URL(endPoint)); // a,b 调⽤此⽅法的参数String result = (String)call.invoke(new Object[]{new Integer(a),new Integer(b)});System.out.println("result is :"+result);} catch (Exception e) {e.printStackTrace();}}}⽅式三:使⽤Dynamic Proxy动态代理import .URL;import javax.xml.*;public class UsingDP {public static void main(String[] args) {try {int a = 100, b=60;String wsdlUrl = "http://198.168.0.88:6888/ormrpc/services/EASLogin?wsdl";String nameSpaceUri = "http://198.168.0.88:6888/ormrpc/services/EASLogin";String serviceName = "EASLoginProxyService";String portName = "EASLogin";ServiceFactory serviceFactory = ServiceFactory.newInstance();Service service = serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri,serviceName)); // 返回值是⾃⼰封装的类AddFunctionServiceIntf adsIntf = (AddFunctionServiceIntf)service.getPort(new QName(nameSpaceUri,portName),AddFunctionServiceIntf.class); System.out.println("result is :"+adsIntf.addInt(a, b));} catch (Exception e) {e.printStackTrace();}}}⽅式四:使⽤httpclient⽅式参考:直接上代码:import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.util.List;import mons.httpclient.HttpClient;import mons.httpclient.methods.InputStreamRequestEntity;import mons.httpclient.methods.PostMethod;import mons.httpclient.methods.RequestEntity;import org.apache.log4j.Logger;import org.dom4j.Document;import org.dom4j.io.SAXReader;// 这⾥引得依赖包的话需要⾃⼰找了下⾯地址可以找到//https:///public static InputStream postXmlRequestInputStream(String requestUrl, String xmlData) throws IOException{PostMethod postMethod = new PostMethod(requestUrl);byte[] b = xmlData.getBytes("utf-8");InputStream is = new ByteArrayInputStream(b, 0, b.length);RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml;charset=utf-8");postMethod.setRequestEntity(re);HttpClient httpClient = new HttpClient();httpClient.getParams().setAuthenticationPreemptive(true);httpClient.getHostConfiguration().setProxy(CommonPptsUtil.get("PROXY_HOST"), Integer.valueOf(CommonPptsUtil.get("PROXY_PORT")));int statusCode = httpClient.executeMethod(postMethod);logger.debug("responseCode:"+statusCode);if (statusCode != 200) {return null;}return postMethod.getResponseBodyAsStream();}public static void main(String[] args) {String reqJsonStr = "{\"workId\":\"20171018161622\",\"status\":\"201\",\"startTime\":\"2017-10-18 16:16:22\"}";String xmlData = "<soapenv:Envelope xmlns:soapenv=\"/soap/envelope/\" xmlns:ser=\"/\"><soapenv:Header/><soapenv:Body><ser:statusWriteBack><jsonString>" + "{\"workId\":\"314\",\"orderId\":\"5207675\",\"longitude\":\"104.068310\",\"latitude\":\"30.539503\",\"sendTime\":\"2019-08-13 08:38:45\",\"servicePerName\":\"于xx\",\"servicePerPhone\":\"184xxxx7680\"}"+ "</jsonString></ser:statusWriteBack></soapenv:Body></soapenv:Envelope>";String url = "http://xx.xxx.246.88:7103/avs/services/CCService?wsdl";SAXReader reader = new SAXReader();String result = "";try {InputStream in = postXmlRequestInputStream(url,xmlData);if(in!=null){Document doc = reader.read(in);result = doc.getRootElement().element("Body").element("statusWriteBackResponse").element("return").getText();logger.debug("result:"+result);}} catch (Exception e) {logger.error("error:",e);e.printStackTrace();}}}CommonPptsUtil://就是获取配置⽂件⾥的代理信息import mons.configuration.ConfigurationException;import mons.configuration.PropertiesConfiguration;import mons.configuration.reloading.FileChangedReloadingStrategy;import org.apache.log4j.Logger;/*** 通⽤属性⽂件⼯具类** @author y.c**/public class CommonPptsUtil {private static final Logger logger = Logger.getLogger(CommonPptsUtil.class);private static final String CONFIG_FILE = "common.properties";private static PropertiesConfiguration ppts;static {try {ppts = new PropertiesConfiguration(CONFIG_FILE);ppts.setReloadingStrategy(new FileChangedReloadingStrategy());} catch (ConfigurationException e) {logger.error("⽂件【common.properties】加载失败!");ppts = null;}}/*** 获取属性值** @param key* @return属性值*/public static String get(String key) {if (ppts == null) {return null;}return ppts.getString(key);}}⽅式五:使⽤CXF动态调⽤webservice接⼝代码如下:参考:package cxfClient;import org.apache.cxf.endpoint.Endpoint;import space.QName;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import org.apache.cxf.service.model.BindingInfo;import org.apache.cxf.service.model.BindingOperationInfo;public class CxfClient {public static void main(String[] args) throws Exception {String url = "http://localhost:9091/Service/SayHello?wsdl";String method = "say";Object[] parameters = new Object[]{"我是参数"};System.out.println(invokeRemoteMethod(url, method, parameters)[0]);}public static Object[] invokeRemoteMethod(String url, String operation, Object[] parameters){JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();if (!url.endsWith("wsdl")) {url += "?wsdl";}org.apache.cxf.endpoint.Client client = dcf.createClient(url);//处理webService接⼝和实现类namespace不同的情况,CXF动态客户端在处理此问题时,会报No operation was found with the name的异常Endpoint endpoint = client.getEndpoint();QName opName = new QName(endpoint.getService().getName().getNamespaceURI(),operation);BindingInfo bindingInfo= endpoint.getEndpointInfo().getBinding();if(bindingInfo.getOperation(opName) == null){for(BindingOperationInfo operationInfo : bindingInfo.getOperations()){if(operation.equals(operationInfo.getName().getLocalPart())){opName = operationInfo.getName();break;}}}Object[] res = null;try {res = client.invoke(opName, parameters); } catch (Exception e) {e.printStackTrace();}return res;}}。

jax-ws,jax-rs规范的webservice客户端调用方式及soap安全验证

jax-ws,jax-rs规范的webservice客户端调用方式及soap安全验证

jax-ws,jax-rs规范的webservice客户端调⽤⽅式及soap安全验证Java调⽤webservice⽅式的总结柿⼦当然要拿软的捏,笔者先讲基于http协议的jax-rs规范的webservice的调⽤⽅式。

客户端调⽤WebService的⽅式:1.通过wximport⽣成代码2.通过客户端编程⽅式(同第⼀种是⼀样都是本地调⽤)3.通过ajax调⽤⽅式(可能存在跨域jax-rs)4.通过URL Connection ⽅式调⽤5.通过HttpClient⽅式调⽤6.xfire框架下⽣成的客户端(不⽤)1.wximport根据wsdl⽂档⽣成客户端代码,再调⽤在eclipse中,根据操作⽣成客户端代码,Eg:调⽤helloWS⽅法即可2.客户单编程⽅式(和第⼀种⽅式⼀样)先⽣成客户端代码后,调⽤以下是经测试后的实例:URL url = new URL("http://localhost:88/webServiceWS/wsWSPort?wsdl");QName sname = new QName("http://ws.webservice.suan/", "wsWSService");Service service = Service.create(url,sname);WsWSDelegate ms = service.getPort(WsWSDelegate.class);System.out.println(ms.helloWS("suansuan"));} catch (MalformedURLException e) {e.printStackTrace();第⼆种⽅式中,还可以直接创建了SOAP消息后使⽤dispatch便可以进⾏传递,通过extractConentAsDocument⽅法得到Document类型的返回值参考⽹页:/doc/e1f26763763231126edb11a2.html /wanghuan203/article/details/92195653.使⽤ajax+xml+js的⽅式调⽤具体使⽤⽅法,参考整理的ajax跨域⽂档4.URL Connection⽅式//服务的地址//服务的地址URL wsUrl = new URL("http://localhost:88/webServiceWS/wsWSPort");HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();conn.setDoInput(true);conn.setDoOutput(true);conn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");OutputStream os = conn.getOutputStream();//创建SOAPMessageSOAPMessage msg=MessageFactory.newInstance().createMessage(); SOAPEnvelope envelope =msg.getSOAPPart().getEnvelope();SOAPBody body=envelope.getBody();//创建QName来指定消息中传递数据QName ename=new QName("http://ws.webservice.suan/","HelloWS","wsWSService");//SOAPBodyElement ele=body.addBodyElement(ename);ele.addChildElement("arg0").setValue("suansuan");String soap1=soap.toSoapString(msg);os.write(soap1.getBytes());InputStream is = conn.getInputStream();byte[] b = new byte[1024];int len = 0;String s = "";while((len = is.read(b)) != -1){String ss = new String(b,0,len,"UTF-8");s += ss;}System.out.println(s);is.close();os.close();conn.disconnect();5.httpclient⽅式需要commons-codec-1.3.jar,commons-logging-1.0.4.jar,commons-httpclient-3.1.jar//定义⼀个PostMethod,这时需要指定web服务的Url//soapRequestData是传递的soap协议的信息,可以通过soap建⽴,也可以直接String PostMethod postMethod =new PostMethod("http://localhost:88/webServiceWS/wsWSPort");byte[] b = soapRequestData.getBytes("utf-8");InputStream is =new ByteArrayInputStream(b,0,b.length);//RequestEntity re = new InputStreamRequestEntity(is,b.length,"application/soap+xml; charset=utf-8");//不能设置后⾯的内容,设置了报415错误RequestEntity re =new InputStreamRequestEntity(is,b.length);postMethod.setRequestEntity(re);HttpClient httpClient =new HttpClient();int statusCode = httpClient.executeMethod(postMethod);//请求状态200 okString result =postMethod.getResponseBodyAsString();//返回的字符串形式的soap,进⼀步解析System.out.println(statusCode);System.out.println(result.toString());6.xfire框架下⽣成的客户端也是通过wsdl⽣成客户端程序后调⽤,⾄于soap header的验证,使⽤map加⼊验证信息后验证具体的操作,在我⼯作⽂档中,有⼀个短信平台接⼝⽂档有详细的xfire的使⽤过程,通⽤性不错注意,以上关于soap信息,我是根据⽣成的本地调⽤类的注释,编写soap信息,此外可以直接使⽤String类型的字符串,只要你熟悉soap的格式,就可以⼿动编写传递的soap消息。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

webservice的调用方式
Web Service的调用方式
Web Service是一种基于互联网的通信协议,通过HTTP和XML 来进行通信。

它可以实现不同平台、不同语言之间的数据交互和共享,为分布式应用提供了一种标准化的通信方式。

在使用Web Service时,我们可以采用不同的调用方式,以满足不同的需求和场景。

1. SOAP方式调用
SOAP(Simple Object Access Protocol)是一种基于XML的消息格式,用于在Web Service之间进行通信。

SOAP方式调用Web Service需要创建SOAP消息,并通过HTTP传输到目标服务端。

SOAP消息包含了调用的方法、参数和返回值等信息。

服务端接收到SOAP消息后,解析其中的内容,并执行相应的方法,然后将结果封装成SOAP消息返回给客户端。

2. RESTful方式调用
REST(Representational State Transfer)是一种基于Web的软件架构风格,它使用简单的URL作为资源的标识符,并通过HTTP 的不同请求方法来对资源进行操作。

RESTful方式调用Web Service时,我们可以使用不同的HTTP方法(如GET、POST、PUT、DELETE)来对资源进行增删改查操作。

客户端通过构建不同
的URL来调用Web Service,服务端根据URL的不同执行相应的操作,并返回结果。

3. JSON-RPC方式调用
JSON-RPC(JavaScript Object Notation Remote Procedure Call)是一种基于JSON格式的远程过程调用协议。

JSON-RPC方式调用Web Service时,客户端将调用的方法、参数等信息封装成JSON格式的消息,并通过HTTP传输到服务端。

服务端接收到消息后,解析其中的内容,并执行相应的方法,然后将结果封装成JSON格式的消息返回给客户端。

4. XML-RPC方式调用
XML-RPC(XML Remote Procedure Call)是一种基于XML格式的远程过程调用协议。

XML-RPC方式调用Web Service时,客户端将调用的方法、参数等信息封装成XML格式的消息,并通过HTTP传输到服务端。

服务端接收到消息后,解析其中的内容,并执行相应的方法,然后将结果封装成XML格式的消息返回给客户端。

5. JAX-RS方式调用
JAX-RS(Java API for RESTful Web Services)是Java EE平台提供的一种用于开发RESTful Web Service的标准API。

使用JAX-RS方式调用Web Service时,我们可以通过注解的方式将Java类和方法映射到URL上,从而实现通过HTTP请求来调用Web
Service。

客户端通过构建不同的URL来调用Web Service,服务端根据URL的不同执行相应的操作,并返回结果。

总结起来,Web Service的调用方式有SOAP方式调用、RESTful 方式调用、JSON-RPC方式调用、XML-RPC方式调用和JAX-RS 方式调用等。

不同的调用方式适用于不同的场景和需求,我们可以根据具体的情况选择合适的调用方式来实现数据的交互和共享。

无论采用哪种调用方式,我们都可以通过HTTP和XML或JSON等格式来进行通信,实现跨平台、跨语言的数据传输和交互。

相关文档
最新文档