四种方式调用webservice
- 格式:doc
- 大小:428.00 KB
- 文档页数:24
现在有几种调用webservice的方法,总结一下:
①stub方式。
这种方式,是利用axis的WSDL2Java工具类。
这个是根据webservice的wsdl生成客户端类。
这样就可以像是在调用本地的类一样来调用webservice,很简单。
需要一个批处理,如下:
②动态代理
2.1. axis1.4
解释一下:编写流程:
1.创建axis的客户端服务service
2.由service创建call(call就是要进行调用的对象)
3.需要设置call对象的属性targetEndpointAddress(目标地址)和operationName(操作方法就是要调用的方法,其中这个方法需要QName(用于定位方法的)对象),new QName(path,”putInfo”)其中path是名称空间,putInfo 是这个名称空间中的一个方法(要调用的方法)。
4.最后就是调用webservice的方法了,invoke,这个方法需要传入需要的传入的参数,并且这个参数以对象数组的方式传过去。
此方法会返回返回值。
2.2 axis2
注释详细,不解释了。
java调用WebService(客户端)看下了网上大部分都是写java来编写WS服务端,小编这边就小写了下JAVA的调用端。
WebService可以有Get、Post、Soap、Document四种方式调用,以下是四种方式的参照说明。
对于SOAP协议多写了个CXF的调用(对于CXF必须有以下包:)以下是调用代码import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import .URL;import .URLConnection;import .URLEncoder;import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;/*** 功能描述:WebService调用**/public class ClientTest {/*** 功能描述:HTTP-GET**/public String get() {URL url;BufferedReader bin = null;StringBuilder result = new StringBuilder();try {String urlTemp = "//WebServices/WeatherWebService.asmx/getSupportCity?byProvin ceName="+ URLEncoder.encode("福建", "UTF-8");// URLEncoder用来参数编码url = new URL(urlTemp);InputStream in = url.openStream(); // 请求bin = new BufferedReader(new InputStreamReader(in, "UTF-8"));String s = null;while ((s = bin.readLine()) != null) {result.append(s);}} catch (Exception e) {e.printStackTrace();} finally {if (null != bin) {try {bin.close();} catch (IOException e) {e.printStackTrace();}}}return result.toString();}/*** 功能描述:HTTP-POST**/public String post() {OutputStreamWriter out = null;StringBuilder sTotalString = new StringBuilder();try {URL urlTemp = new URL("/WebServices/WeatherWebService.asmx/getSupportCity");URLConnection connection = urlTemp.openConnection();connection.setDoOutput(true);out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");StringBuffer sb = new StringBuffer();sb.append("byProvinceName=福建");out.write(sb.toString());out.flush();String sCurrentLine;sCurrentLine = "";InputStream l_urlStream;l_urlStream = connection.getInputStream();// 请求BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));while ((sCurrentLine = l_reader.readLine()) != null) {sTotalString.append(sCurrentLine);}} catch (Exception e) {e.printStackTrace();} finally {if (null != out) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}return sTotalString.toString();}/*** 功能描述:请求HTTP-SOAP**/public String getSoapInputStream() {try {String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"/2001/XMLSchema-instance\"xmlns:xsd=\"/2001/XMLSchema\"xmlns:soap=\"/soap/envelope/\"><soap:Body><getSupportCity xmlns=\"/\"><byProvinceName></byProvinceName></getSupportCity></ soap:Body></soap:Envelope>";URL url = new URL("/WebServices/WeatherWebService.asmx?wsdl");URLConnection 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=utf-8");conn.setRequestProperty("SOAPAction","/getSupportCity");OutputStream os = conn.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");osw.write(soap);osw.flush();osw.close();StringBuilder sTotalString = new StringBuilder();String sCurrentLine = "";InputStream is = conn.getInputStream();BufferedReader l_reader = new BufferedReader(new InputStreamReader(is));while ((sCurrentLine = l_reader.readLine()) != null) {sTotalString.append(sCurrentLine);}return sTotalString.toString();} catch (Exception e) {e.printStackTrace();return null;}}/*** 功能描述:使用CXF 请求HTTP-SOAP**/public String soap() {JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();String url = "/webservices/qqOnlineWebService.asmx?wsdl";//:17001/DefDispatcher/dispatche?wsdlClient clientTemp = clientFactory.createClient(url);Object[] arg;String result = "";try {arg = clientTemp.invoke("qqCheckOnline", "8698053");// 查询8698053在线状态,QQ号码// String,默认QQ号码:8698053。
Webservice接口请求方法一、概述Webservice是一种基于网络的应用程序接口(API),它能够通过网络来创建和提供服务。
在日常开发中,我们经常需要与其他系统进行交互,并获取相应的数据或服务。
Webservice接口请求方法就是用来实现这一目的的。
二、Webservice接口请求的方式1. HTTP GET请求:当我们需要从Webservice接口获取数据时,可以使用HTTP GET请求。
通过在URL中附加相应的参数,可以向服务端发送请求,并获取相应的数据。
GET请求的参数会以键值对的形式出现在URL中,例如:xxx。
2. HTTP POST请求:如果需要向Webservice接口提交数据,可以使用HTTP POST请求。
通过向服务端发送POST请求,我们可以将数据放入请求的body中,并提交给服务端进行处理。
3. SOAP请求:SOAP(Simple Object Access Protocol)是一种基于XML的通信协议,通过SOAP请求可以实现不同评台之间的通信。
这种方式一般需要事先定义好XML格式的请求数据,并通过HTTP传输到服务端。
4. RESTful请求:RESTful(Representational State Transfer)是一种基于HTTP协议的设计风格,通过RESTful请求可以实现对资源的增删改查操作。
在RESTful请求中,通常使用HTTP的不同方法(如GET、POST、PUT、DELETE)来实现相应的操作。
三、Webservice接口请求的参数1. 请求URL:Webservice接口的请求位置区域,即服务端提供的接口位置区域。
2. 请求方法:即使用的HTTP方法,可以是GET、POST、PUT或DELETE等。
3. 请求头部:一些额外的请求头部信息,如Content-Type、Authorization等。
4. 请求参数:需要传递给服务端的参数,可以是键值对、JSON格式或XML格式的数据。
调用webservice的几种方式webservice是一种服务,它可以让不同类型的应用程序通过网络交换数据和信息,通过联网,客户可以与服务器端通信,而服务器端可以生成一些特定的网络服务。
webservice使得手机端客户可以实时调用服务器端的接口,从而获取相应的资源和信息,也就是说客户和服务器端之间的计算机网络协议可以实现webservice方式的交互。
通常情况下,webservice有几种不同的实现方式。
下面简单介绍几种可以实现webservice的方式。
1. 使用Axios或低层Javascript原生api发起HTTP请求。
Axios是一个支持浏览器和node.js的客户端框架,用来发起http请求。
它可以实现前后端数据交互,实现webservice。
2. 使用传统soap协议发起HTTP请求。
soap(simple object access protocol)是一种基于XML的协议,它可以通过HTTP使得应用程序交互,它可以使用简单的消息来交互数据,而不用编写特定的代码。
3. 使用REST协议发起HTTP请求。
REST协议是一种使用RESTful API的软件设计架构,它是用来构建及交互分布式系统的一种设计方法。
4. 使用JSON API发起HTTP请求。
JSON API是一种用以支持数据并行关系的API,可以让JSON数据分发,并且提供资源定位、数据多态、查询、及内容引用等功能,从而构建实现交互的webservice系统。
5. 使用gRPC发送HTTP请求。
gRPC是一种开放源的远程过程调用(RPC)系统,可以通过http2发起请求。
它的实现允许你在任何语言之间实现客户端/服务器端通信,以及实现webservice功能。
以上就是实现web service的几种方式,它们可以帮助我们将不同的应用程序通过网络交换数据和信息,实现webservice的功能,它可以帮助我们更好地构建客户端/服务器端通信环境,让客户可以实时调用服务器端接口,实现我们想要的功能,方便地实现我们服务器与客户端之间的交互和通信。
【JavaEE学习80下】【调⽤WebService服务的四种⽅式】【WebService。
不考虑第三⽅框架,如果只使⽤JDK提供的API,那么可以使⽤三种⽅式调⽤WebService服务;另外还可以使⽤Ajax调⽤WebService 服务。
预备⼯作:开启WebService服务,使⽤jdk命令wsimport⽣成调⽤源代码package com.kdyzm.ws;import javax.jws.WebService;import javax.xml.ws.Endpoint;@WebServicepublic class MyWsServer {public String calculate(int input){System.out.println("接收到请求数据:"+input);return input*input+"";}public static void main(String[] args) {Endpoint.publish("http://localhost:9090/ws", new MyWsServer());System.out.println("server ready ......");}} ⽣成源代码命令:wsimport -s . http://localhost:9090/ws?wsdl 可能出现的问题参考: 因为出现了上述问题,所以本次测试环境使⽤jdk 1.7。
⽅法⼀:使⽤最简单、效率最⾼的⽅法调⽤WebService服务 将⽣成的java⽂件包括⽂件夹直接导⼊项⽬,并使⽤其提供的API。
1package com.kdyzm.call.method;23import com.kdyzm.ws.MyWsServer;4import com.kdyzm.ws.MyWsServerService;56/**7 * 第⼀种⽅式就是使⽤wsimport命令获取所有的需要调⽤的代码,并直接使⽤这些代码完成任务8 * @author kdyzm9 *10*/11public class Method1 {12public static void main(String[] args) {13 MyWsServerService myWsServerService=new MyWsServerService();14 MyWsServer myWsServer=myWsServerService.getMyWsServerPort();15 String result=myWsServer.calculate(2);16 System.out.println(result);17 }18 } 客户端控制台打印结果:4 服务端控制台打印结果: 这种⽅式是使⽤最多的⽅式,也是最不容易出错、效率最⾼的⽅式,推荐使⽤这种⽅式。
四种方式调用webservice四种客户端调用WEBSERVICE 王凡***************************************2009-08-22目录1.文档概述 (2)1.1.相关文档 (2)2.SERVER端 (3)2.1.代码 (3)2.2.SERVER端生成Wsdl (4)2.3.服务端安全性设置 (6)3.VB调用 (13)3.1.准备 (13)调用 (16)4.1.实现过程 (16)5.Axis2调用 (17)5.1.Axis2的安装 (17)5.2.Axis2调用过程 (17)6.Weblogic调用 (21)6.1.Weblogic (21)7.已解决及未解决的问题 ........................................................... 错误!未定义书签。
7.1.未解决的问题.................................................................... 错误!未定义书签。
7.2.已解决的问题.................................................................... 错误!未定义书签。
1.文档概述1.1.相关文档《Web Services开发教程.doc》2.SERVER端这个主要是用weblogic的workshop开发,用weblogic开发webservice具体可见沈辉的《Web Services开发教程.doc》,这里主要是讲客户端调用,所以服务端只作罗列。
2.1.代码在weblogic中构建webservice可以见《Web Services开发教程.doc》,用于本次测试的Server代码如下:Model:package .tsingtao.logistics.model;import java.io.Serializable;public class test implements Serializable {private static final long serialVersionUID = 1L;private String NAME;private int AGE;public void setNAME(String name){NAME=name;}public void setAGE(int age){AGE=age;}public String getNAME(){return NAME;}public int getAGE(){return AGE;}}Service代码如下:public .tsingtao.logistics.model.test[] hello(String NAME,int AGE) {.tsingtao.logistics.model.test[] t=new.tsingtao.logistics.model.test[10];for(int i=0;i<10;i++){t[i] = new .tsingtao.logistics.model.test();t[i].setNAME(NAME+i);t[i].setAGE(AGE+i);}return t;}2.2.SERVER端生成Wsdl在Server端生成的wsd如下:<="" bdsfid="134" name="testServiceDefinitions" p="">targetNamespace="http://cn/com/tsingtao/logistics/service " xmlns=""xmlns:s0="/wsdl/"xmlns:s1="http://cn/com/tsingtao/logistics/service"xmlns:s2="/wsdl/soap/"><="" attributeformdefault="unqualified" bdsfid="142" elementformdefault="qualified" p="">targetNamespace="java:.tsingtao.logistics.model"xmlns:s0="/wsdl/"xmlns:s1="http://cn/com/tsingtao/logistics/service"xmlns:s2="/wsdl/soap/"xmlns:xs="/2001/XMLSchema"><="" bdsfid="163" maxoccurs="unbounded" minoccurs="0" name="test" p="">nillable="true" type="java:test" xmlns:java="java:.tsingtao.logistics.model"/><="" bdsfid="168" name="ArrayOftest_literal" p="" type="java:ArrayOftest_literal">xmlns:java="java:.tsingtao.logistics.model"/><="" attributeformdefault="unqualified" bdsfid="172" elementformdefault="qualified" p="" targetnamespace="http://cn/com/tsingtao/logistics/service"> xmlns:s0="/wsdl/"xmlns:s1="http://cn/com/tsingtao/logistics/service"xmlns:s2="/wsdl/soap/"xmlns:xs="/2001/XMLSchema"><="" bdsfid="199" name="return" p="" type="java:ArrayOftest_literal">xmlns:java="java:.tsingtao.logistics.model"/><="" bdsfid="229" p="" style="document">transport="/soap/http"/>2.3.服务端安全性设置这个是根据沈辉的《Web Services开发教程.doc》设置的,不作详细说明,只作罗列2.3.1.在weblogic中新建用户和用户组2.3.2.在webapp中配置安全性在web-info\web.xml中添加红色部分<="" bdsfid="278" id="WebApp_ID" p="" version="2.4">xmlns="/xml/ns/j2ee"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/j2ee /xml/ns/j2ee/web-app_2_4.xsd"> BSMindex.htmlindex.htmindex.jspdefault.htmldefault.htmdefault.jspwsuserSecure Area/*GETPOSTwsuserBASICmyrealm在web-info\weblogic.xml中添加红色部分<wls:weblogic-web-app< bdsfid="334" p=""></wls:weblogic-web-app<>xmlns:wls="/ns/weblogic/weblogic-web-app"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/javaee/xml/ns/javaee/web-app_2_5.xsd/ns/weblogic/weblogic-web-app/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">10.3BSMwsuserwsgroup重新发布后在浏览器中就可以看到了3.VB调用3.1.准备使用vb6.0调用webservice需要用到SOAP T oolkit,我们选用的是SOAP Toolkit3.0版本。
C#通过Get、Post、Soap调⽤WebService的⽅法实现代码来源于⽹络,我只是作了⼀些修改!using System;using System.Web;using System.Xml;using System.Collections;using ;using System.Text;using System.IO;using System.Xml.Serialization;/// <summary>/// 利⽤WebRequest/WebResponse进⾏WebService调⽤的类/// </summary>public class WebServiceHelper{//<webServices>// <protocols>// <add name="HttpGet"/>// <add name="HttpPost"/>// </protocols>//</webServices>private static Hashtable _xmlNamespaces = new Hashtable();//缓存xmlNamespace,避免重复调⽤GetNamespace/// <summary>/// 需要WebService⽀持Post调⽤/// </summary>public static XmlDocument QueryPostWebService(String URL, String MethodName, Hashtable Pars){HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);request.Method = "POST";request.ContentType = "application/x-www-form-urlencoded";SetWebRequest(request);byte[] data = EncodePars(Pars);WriteRequestData(request, data);return ReadXmlResponse(request.GetResponse());}/// <summary>/// 需要WebService⽀持Get调⽤/// </summary>public static XmlDocument QueryGetWebService(String URL, String MethodName, Hashtable Pars){HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + ParsToString(Pars));request.Method = "GET";request.ContentType = "application/x-www-form-urlencoded";SetWebRequest(request);return ReadXmlResponse(request.GetResponse());}/// <summary>/// 通⽤WebService调⽤(Soap),参数Pars为String类型的参数名、参数值/// </summary>public static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars){if (_xmlNamespaces.ContainsKey(URL)){return QuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces[URL].ToString());}else{return QuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL));}}/// <summary>/// 通⽤WebService调⽤(Soap)/// </summary>/// <param name="URL"></param>/// <param name="MethodName"></param>/// <param name="Pars"></param>/// <param name="XmlNs"></param>/// <returns></returns>private static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars, string XmlNs){_xmlNamespaces[URL] = XmlNs;//加⼊缓存,提⾼效率HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);request.Method = "POST";request.ContentType = "text/xml; charset=utf-8";request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");SetWebRequest(request);byte[] data = EncodeParsToSoap(Pars, XmlNs, MethodName);WriteRequestData(request, data);XmlDocument doc = new XmlDocument(), doc2 = new XmlDocument();doc = ReadXmlResponse(request.GetResponse());XmlNamespaceManager mgr = new XmlNamespaceManager(Table);mgr.AddNamespace("soap", "/soap/envelope/");String RetXml = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;doc2.LoadXml("<root>" + RetXml + "</root>");AddDelaration(doc2);return doc2;}/// <summary>/// 通过WebService的WSDL获取XML名称空间/// </summary>/// <param name="URL"></param>/// <returns></returns>private static string GetNamespace(String URL){HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");SetWebRequest(request);WebResponse response = request.GetResponse();StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);XmlDocument doc = new XmlDocument();doc.LoadXml(sr.ReadToEnd());sr.Close();return doc.SelectSingleNode("//@targetNamespace").Value;/// 动态⽣成SOP请求报⽂内容/// </summary>/// <param name="Pars"></param>/// <param name="XmlNs"></param>/// <param name="MethodName"></param>/// <returns></returns>private static byte[] EncodeParsToSoap(Hashtable Pars, String XmlNs, String MethodName){XmlDocument doc = new XmlDocument();doc.LoadXml("<soap:Envelope xmlns:xsi=\"/2001/XMLSchema-instance\" xmlns:xsd=\"/2001/XMLSchema\" xmlns:soap=\"/soap/envelope/\"></soap:Envelope>"); AddDelaration(doc);XmlElement soapBody = doc.CreateElement("soap", "Body", "/soap/envelope/");XmlElement soapMethod = doc.CreateElement(MethodName);soapMethod.SetAttribute("xmlns", XmlNs);foreach (string k in Pars.Keys){XmlElement soapPar = doc.CreateElement(k);soapPar.InnerXml = ObjectToSoapXml(Pars[k]);soapMethod.AppendChild(soapPar);}soapBody.AppendChild(soapMethod);doc.DocumentElement.AppendChild(soapBody);return Encoding.UTF8.GetBytes(doc.OuterXml);}/// <summary>/// 将对象转换成XML节点格式/// </summary>/// <param name="o"></param>/// <returns></returns>private static string ObjectToSoapXml(object o){XmlSerializer mySerializer = new XmlSerializer(o.GetType());MemoryStream ms = new MemoryStream();mySerializer.Serialize(ms, o);XmlDocument doc = new XmlDocument();doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));if (doc.DocumentElement != null){return doc.DocumentElement.InnerXml;}else{return o.ToString();}}/// <summary>/// 设置WEB请求/// </summary>/// <param name="request"></param>private static void SetWebRequest(HttpWebRequest request){request.Credentials = CredentialCache.DefaultCredentials;request.Timeout = 10000;}/// <summary>/// 设置请求数据/// </summary>/// <param name="request"></param>/// <param name="data"></param>private static void WriteRequestData(HttpWebRequest request, byte[] data){request.ContentLength = data.Length;Stream writer = request.GetRequestStream();writer.Write(data, 0, data.Length);writer.Close();}/// <summary>/// 获取字符串的UTF8码字符串/// </summary>/// <param name="Pars"></param>/// <returns></returns>private static byte[] EncodePars(Hashtable Pars){return Encoding.UTF8.GetBytes(ParsToString(Pars));}/// <summary>/// 将Hashtable转换成WEB请求键值对字符串/// </summary>/// <param name="Pars"></param>/// <returns></returns>private static String ParsToString(Hashtable Pars){StringBuilder sb = new StringBuilder();foreach (string k in Pars.Keys){if (sb.Length > 0){sb.Append("&");}sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));}return sb.ToString();}/// <summary>/// 获取Webservice响应报⽂XML/// </summary>/// <param name="response"></param>/// <returns></returns>private static XmlDocument ReadXmlResponse(WebResponse response){StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);String retXml = sr.ReadToEnd();sr.Close();XmlDocument doc = new XmlDocument();doc.LoadXml(retXml);return doc;/// 设置XML⽂档版本声明/// </summary>/// <param name="doc"></param>private static void AddDelaration(XmlDocument doc){XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);doc.InsertBefore(decl, doc.DocumentElement);}}以上是在不引⽤WEB服务的条件下动态调⽤WEB服务⽅法,如果该服务地址不确定或后期有可能会变动,则采⽤上述这些⽅法会⽐较好,当然如果服务地址不会出现变更的情况下,可以在VS中直接引⽤WEB服务,然后就可调⽤⾃动⽣成的SOAPClient类来调⽤相应的WEB服务⽅法,这样就简单很多,如下:avar client = new SmsService.wmgwSoapClient(); //wmgwSoapClient就是引⽤WEB服务后⾃动⽣成的类string result = client.MongateSendSubmit(tuserId.Text, tpassword.Text, tpszMobis.Text, tpszMsg.Text, int.Parse(tiMobiCount.Text),tpszSubPort.Text, tMsgId.Text);。
WebService 四种发布方式总结Author:yczhang1.CXF方式CXF与spring搭建webservice是目前最流行的方式,但是传闻cxf与jdk1.5有些不兼容,我没有遇到过,我遇到的问题是cxf与was6.1.1不兼容,表现在cxf必须的jar包“wsdl4j-1.6.2.jar”报错,报的错为:ng.IncompatibleClassChangeError,明显的jar包不兼容问题,很是头痛,后来查找资料找到解决办法是,将上述jar包新建一个was共享库,可以解决,但是客户周经理不想用此种方式,因为需要修改was,于是改用了axis2方式,下文会介绍。
该问题在此处做个记录,以后使用cxf与was的时候需要注意!!!使用cxf+spring搭建WebService:第一步,添加jar包。
此处需要注意,不同环境(tomcat、was)jar也不一定相同,例如我本地cxf+spring只需要如下jar包:而泰康的was环境则需要如下jar包:明显的多了很多,原因应该是服务器jar包池的不同。
根据错误提示缺什么补什么就可以了,注意jar包勿重复。
第二步,配置web.xml文件,如下(重要的地方已标记):<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/applicationContext.xml</param-value> </context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener </listener-class></listener><!-- Character Encoding filter --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFil ter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-c lass><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/webservice/*</url-pattern></servlet-mapping>上述标记的地方,第一处是spring配置文件路径;第二出是wsdl地址内容;第三步,编写接口类与实现类,注意注解接口类@WebServicepublic interface SendService {public boolean sendOA(@WebParam(name="param")String param);public boolean sendOrg(OrgEntity org);}实现类@WebService(endpointInterface="com.service.SendService",serviceName=" sendService")public class SendServiceImpl implements SendService{public boolean sendOA(String param) {System.out.println("-------sendOA---------param:"+param);if(param.equals("zhoujian")){return true;}return false;}public boolean sendOrg(OrgEntity org) {System.out.println("-------sendOrg--begin-------");return true;}}第四步,Spring配置文件“jaxws:client”该标签可以不必写,访问时可以手动拼接该url 第五步,发布,直接部署到服务器,访问:2.Xfire方式据说xfire方式已经很老了,但个人感觉,xfire方式很简单且容易配置,不知为啥过时了,也没感觉cxf、axis2哪里先进,我当时卡在cxf与was搞不定时想尝试xfire方式被周经理给拒绝了。
java调⽤WebService服务的四种⽅法总结⽬录⼀、前⾔⼆、简介三、具体解析第⼀种⽅式,⾸先得下载axis2的jar包,Axis2提供了⼀个wsdl2java.bat命令可以根据WSDL⽂件⾃动产⽣调⽤WebService的代码。
第⼆种RPC ⽅式,强烈推荐。
第三种:利⽤HttpURLConnection拼接和解析报⽂进⾏调⽤。
第四种,利⽤httpclient总结⼀、前⾔本来不想写这个的,因为⽹上类似的是在是太多了。
但是想想⾃⼰前⾯段时间⽤过,⽽且以后可能再也没机会⽤了。
所以还是记录⼀下吧。
我这⼉是以C语⾔⽣成的WebService为例。
因为通常来说,两个java端之间的互相通讯没必要写成WebService的⽅式,太⿇烦。
除⾮有⼀⽅已经固定了是webService的⽅式(常见于⽜逼的甲⽅)。
⽽且就算写成了WebService⽅式两个java端直接调⽤也相对⽐较简单,因为⽤java的话很多规范都是⾃动⽣成好的,⽽其他语⾔就不是这样了,有时候对⽅压根就不是正确的规范,你还不能让对⽅改我觉得webService这个东西常⽤于不同语⾔编写的服务器之间进⾏数据交互。
因为是基于WSDL的。
我知道的主要的⽅法有以下⼏种(我这⼉全部以C语⾔编写的WebService做优缺点⽐较):⼆、简介1、通过axis2将WebService提供的wsdl⽂件⽣成对应的java类,这样就可以相当调⽤本地类⼀样调⽤webService提供的接⼝。
优点:调⽤简单,⽆需⾃⼰编写太多的东西。
缺点:⼤部分情况根据对应的webService⽣成的服务中地址是固定的,不易更改,⽽且⽣成的代码过于庞⼤,不便于阅读。
同时必须得有webservice对应的的wsdl⽂件,不太可控。
2、通过RPC⽅式调⽤(推荐使⽤)优点:⾃⼰编写部分调⽤代码,可灵活更换调⽤的路径,适合分布式部署的服务器,只需要知道webservice服务的⽅法名、命名空间、以及对应的参数就好。
在.NET中,有多种方式可以调用Web Service。
以下是一些常见的方法:
1. 直接把Web Service当做普通的类来引用。
如果调用者和被调用的Web Service将来会部署在一个服务器上,这是一种简单的方法。
2. 通过Visual Studio添加Web Reference。
这时,VS会自动下载WSDL并生成一个客户端代理类。
这种方法操作简单,并且VS自动生成了各种同步和异步方法/事件,功能比较丰富。
但是,如果哪天Web服务的地址变了,需要更新引用并重新编译代码,这可能不方便。
3. 动态调用Web Service。
通过URL,自己实现一个代理类,或者通过URL下载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;}}。
常用调用webservice的方法全文共四篇示例,供读者参考第一篇示例:在现今的软件开发领域中,调用Webservice已经成为一种常见的做法。
Webservice是一种通过网络进行数据交换的技术,可以让应用程序在不同的机器之间进行通信,使得不同的系统可以共享数据和功能。
在本文中,我们将介绍一些常用的调用Webservice的方法以及一些注意事项。
## 一、SOAP协议SOAP(Simple Object Access Protocol)是一种通用的传输协议,可以通过HTTP进行数据交换。
在调用Webservice时,我们可以使用SOAP协议来进行数据交互。
通常情况下,我们可以使用SOAP客户端来发送SOAP请求并接收返回的SOAP响应。
```javaimport javax.xml.soap.*;import java.io.*;## 二、RESTful API```javaimport java.io.BufferedReader;import java.io.InputStreamReader;import .HttpURLConnection;import .URL;## 三、使用开源库除了手动编写代码来调用Webservice,我们还可以使用一些现成的开源库来简化这个过程。
Apache CXF是一个流行的Webservice框架,它提供了丰富的API和工具来帮助我们开发和调用Webservice。
HelloWorld client = (HelloWorld) factory.create();String result = client.sayHello("world");System.out.println(result);}}```## 四、安全注意事项在调用Webservice时,我们也需要考虑安全因素。
我们可以使用HTTPS来保护数据的传输安全,可以使用WS-Security来保护数据的机密性和完整性,还可以使用OAuth来进行身份验证和授权。
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格式的消息返回给客户端。
Visual Studio Code是一款功能强大的集成开发环境,它支持多种编程语言,包括Java。
在Java开发过程中,经常需要调用webservice方法来实现与其他系统的交互,本文将详细介绍在Visual Studio Code中如何调用webservice方法的各种方法。
1. 使用Axis2插件Axis2是一个流行的Java web服务框架,Visual Studio Code提供了Axis2插件来简化调用webservice方法的过程。
首先需要在Visual Studio Code中安装Axis2插件,然后在项目中创建一个webservice客户端,通过Axis2插件生成客户端代码,最后调用webservice方法并处理返回结果。
2. 使用JAX-WSJAX-WS是Java API for XML Web Services的缩写,它提供了一种简单的方式来创建和调用webservice方法。
在Visual Studio Code中,可以使用JAX-WS来生成webservice客户端代码,并通过调用客户端代码来实现与webservice的交互。
需要注意的是,JAX-WS需要依赖一些Java EE的库,在使用时需要将相关的库文件添加到项目中。
3. 使用第三方库除了Axis2和JAX-WS之外,还可以使用一些第三方的Java库来简化调用webservice方法的过程。
例如,Apache CXF是一个流行的Java webservice框架,它提供了丰富的工具和库来简化webservice的开发和调用。
在Visual StudioCode中,可以使用Apache CXF来创建webservice客户端,并通过调用客户端代码来实现与webservice的交互。
4. 使用Spring框架Spring框架是一个流行的Java开发框架,它提供了丰富的工具和库来简化Java开发过程。
在Visual Studio Code中,可以使用Spring框架来创建webservice客户端,并通过调用客户端代码来实现与webservice的交互。
标题:探究Webservice接口调用方式简述1. 了解Webservice接口Webservice是一种基于XML和HTTP协议的分布式服务通信技术,它可以通过网络提供服务、适应各种操作系统和编程语言,并且具有较高的互操作性。
2. Webservice接口的调用方式在使用Webservice接口时,常见的调用方式有两种:SOAP协议和RESTful风格。
3. SOAP协议调用方式SOAP(Simple Object Access Protocol)是一种基于XML的通讯协议,它提供了一种在分布式环境中交换信息的简单、轻量级的方式。
调用SOAP协议的Webservice接口需要事先定义好接口的描述信息,例如WSDL(Web Services Description Language)文件,并通过SOAP消息格式进行数据交换。
4. RESTful风格调用方式REST(Representational State Transfer)是一种基于资源的架构风格,它强调URL定位资源、使用HTTP动词操作资源、使用不同的表示方式来操作资源等原则。
调用RESTful风格的Webservice接口更加灵活简洁,通常是通过HTTP的GET、POST、PUT、DELETE等动词来实现。
5. 对比两种调用方式SOAP协议调用方式相对于RESTful风格而言更加规范、功能丰富,但也更加复杂冗余;而RESTful风格调用方式更加简洁、灵活,但也有一定的局限性。
在选择调用方式时,需要根据具体的业务需求来权衡利弊。
6. 个人观点和理解我个人认为,选择Webservice接口的调用方式需要充分考虑项目需求、技术栈、团队能力等因素。
SOAP协议适用于对安全性和可靠性要求较高的场景,而RESTful风格则更适合于移动端、轻量级应用的开发。
未来随着云计算和微服务架构的发展,RESTful风格的Webservice接口可能会更加普及。
Webservice接口调用方式简介一、什么是Webservice接口?Webservice是一种基于网络的通信协议,用于不同应用程序之间的数据交换。
它使用标准的HTTP协议传输数据,并通过XML格式进行数据封装和传递。
Webservice接口提供了一种跨平台、跨语言的数据交互方式,使得不同系统之间可以方便地进行数据交流和共享。
二、Webservice接口的调用方式Webservice接口的调用方式有多种,常见的有以下几种:1. SOAP调用方式SOAP(Simple Object Access Protocol)是一种基于XML的通信协议,用于在Web上交换结构化的和类型化的信息。
SOAP调用方式是Webservice接口最常见的调用方式之一。
SOAP调用方式的特点是使用XML格式封装请求和响应数据,并使用HTTP协议进行传输。
在SOAP调用中,客户端通过发送一个SOAP请求给服务端,服务端接收到请求后进行处理,并将处理结果封装在一个SOAP响应中返回给客户端。
SOAP调用方式的优点是使用了标准的HTTP协议和XML格式,具有跨平台、跨语言的特性,可以方便地实现不同系统之间的数据交换和共享。
2. RESTful调用方式RESTful(Representational State Transfer)是一种基于HTTP协议的架构风格,用于构建网络上的分布式系统。
RESTful调用方式是Webservice接口中另一种常见的调用方式。
RESTful调用方式的特点是使用HTTP的GET、POST、PUT、DELETE等方法来进行资源的操作。
客户端通过发送HTTP请求给服务端,服务端根据请求的方法进行相应的处理,并将处理结果以HTTP响应的形式返回给客户端。
RESTful调用方式的优点是简单、轻量级,易于理解和使用。
它适用于构建简单的、无状态的接口,特别适合移动应用和Web前端的调用。
3. JSON-RPC调用方式JSON-RPC(Remote Procedure Call)是一种基于JSON格式的远程过程调用协议,用于在网络上调用远程方法。
C#调⽤WebService的三种⽅法最近⽤到了webService的调⽤,因为是远程调⽤,并且对⽅的webService是⽤Java写的,开发过程遇到很多问题,现在把⽤到的⽅法总结⼀下。
1.静态调⽤这是最便捷的⽅法,分为直接使⽤webService地址调⽤,和使⽤本地xml⽂件调⽤。
如下图,如果直接调⽤?wsdl的地址,就把地址复制黏贴到地址框⾥,如果本地没法连接到⽹址,就把?wsdl的地址⽤浏览器打开,保存成xml⽂件,拷贝到本地,再把本地xml⽂件的地址【类似C:\Users\admin\Desktop\XX.xml】复制到下图的地址框⾥,然后点击转到,会在绿⾊的框⾥显⽰:“在地址XX处找到1个服务”,然后修改下命名空间【可以不改】,点击确认。
然后在代码的【Connected Services】⽂件夹下,可以找到⼀个【Reference.cs】的⽂件,这⾥⾯就有引⽤的webService的⽅法。
调⽤的⽅法很简单,在Reference.cs⾥找到开发⽂档⾥提到的⽅法,正常实例化,调⽤就可以,需要引⽤下图蓝框⾥定义的命名空间。
另外,如果调⽤时出现这样的报错:【基础连接已关闭:未能为SSL/TLS安全通道建⽴信任关系】,可以按下⾯三步解决,(1)需要在【Reference.cs】⾥添加引⽤:using ;using .Security;using System.Security.Authentication;using System.Security.Cryptography.X509Certificates;(2)在调⽤的类⾥添加⽅法:private static bool RemoteCertificateValidate(object sender, X509Certificate cert,X509Chain chain, SslPolicyErrors error){System.Console.WriteLine("Warning, trust any certificate");// trust any certificatereturn true;//为了通过证书验证,总是返回true}(3)在类的构造⽅法调⽤上⾯⽅法:ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;//验证服务器证书回调⾃动验证2.动态调⽤动态调⽤需要借助帮助类,这⾥⾯我已经加上了解决【SSL/TLS】问题的代码,代码如下。
webservice调用具体方法摘要:1.什么是WebService2.如何调用WebService的具体方法3.调用过程中可能遇到的问题及解决方法4.实战案例分享正文:WebService,即网络服务,是一种跨平台的、基于XML(可扩展标记语言)的分布式计算技术。
它使得应用程序之间可以通过互联网进行通信,实现数据的交换和共享。
在实际开发中,调用WebService的具体方法如下:1.首先,确定你需要调用的WebService的地址和协议。
通常,WebService的地址是以WSDL(Web服务描述语言)文件形式提供的。
你可以通过WSDL文件查看WebService提供的接口和方法。
2.根据WSDL文件,生成客户端代码。
这可以通过使用诸如Apache CXF、JAX-WS等开源库来实现。
这些库会根据WSDL文件自动生成客户端代码,包括调用WebService的方法和处理响应的代码。
3.在生成的客户端代码中,找到调用WebService的具体方法。
这个方法通常是一个带有HTTP请求头的Web方法,如POST、GET等。
在方法中,传入所需的参数,这些参数通常包括在WSDL文件中定义的输入参数。
4.发送HTTP请求,并将响应结果解析为JSON、XML等格式。
这一步可以通过使用如Jackson、Gson等库来实现。
5.处理响应结果,并根据需要进行相应的业务逻辑处理。
在调用WebService的过程中,可能会遇到以下问题:1.网络问题:由于WebService通常跨网络调用,因此可能会遇到网络不稳定、延迟等问题。
为了解决这个问题,可以考虑使用重试策略、增加超时设置等方法。
2.服务异常:WebService可能会因为各种原因抛出异常,如参数不合法、服务器故障等。
为了解决这个问题,可以实现异常处理机制,如使用try-catch语句捕获异常,并根据需要进行日志记录、通知等操作。
3.身份验证和授权问题:为了避免unauthorized access,WebService 通常需要进行身份验证和授权。
一、利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务1.首先建立一个Web services EndPoint:java 代码[dai ma]package Hello;import javax.jws.WebService;import javax.jws.WebMethod;import javax.xml.ws.Endpoint;@WebServicepublic class Hello {@WebMethodpublic String hello(String name) {return "Hello, " + name + "\n";}public static void main(String[] args) {// create and publish an endpointHello hello = new Hello();Endpoint endpoint = Endpoint.publish(" http://localhost:8080/hello ", hello);}}package Hello;import javax.jws.WebService;import javax.jws.WebMethod;import javax.xml.ws.Endpoint;@WebServicepublic class Hello {@WebMethodpublic String hello(String name) {return "Hello, " + name + "\n";}public static void main(String[] args) {// create and publish an endpointHello hello = new Hello();Endpoint endpoint = Endpoint.publish(" http://localhost:8080/hello ", hello);}}2.使用 apt 编译[bian yi] Hello.java(例:apt -d [存放编译[bian yi]后的文件[wen jian]目录[wen jian mu lu]] Hello.java ) ,会生成 jaws目录3.使用java Hello.Hello运行[yun hang],然后将浏览[liu lan]器[liu lan qi]指向[zhi xiang] http://localhost:8080/hello?wsdl 就会出现下列显示[xian shi]4.使用wsimport 生成客户[ke hu]端使用如下:wsimport -p . -keep http://localhost:8080/hello?wsdl5.客户[ke hu]端程序:Java 代码[dai ma]class HelloClient{public static void main(String args[]) {HelloService service = new HelloService();Hello helloProxy = service.getHelloPort();String hello = helloProxy.hello("你好");System.out.println(hello);}}class HelloClient{public static void main(String args[]) {HelloService service = new HelloService();Hello helloProxy = service.getHelloPort();String hello = helloProxy.hello("你好");System.out.println(hello);}}二、使用xfire,我这里使用的是myeclipse集成[ji cheng]的xfire进行测试的利用xfire开发WebService,可以有三种方法[fang fa]:1一种是从javabean 中生成;2 一种是从wsdl文件[wen jian]中生成;3 还有一种是自己建立webservice步骤如下:用myeclipse建立webservice工程,目录结构[jie gou]如下:首先建立webservice接口[jie kou],代码[dai ma]如下:Java代码[dai ma]package com.myeclipse.wsExample;//Generated by MyEclipsepublic interface IHelloWorldService {public String example(String message);}package com.myeclipse.wsExample;//Generated by MyEclipsepublic interface IHelloWorldService {public String example(String message);}Java代码[dai ma]package com.myeclipse.wsExample;//Generated by MyEclipsepublic class HelloWorldServiceImpl implements IHelloWorldService { public String example(String message) {return message;}}package com.myeclipse.wsExample;//Generated by MyEclipsepublic class HelloWorldServiceImpl implements IHelloWorldService { public String example(String message) {return message;}}修改[xiu gai]service.xml 文件[wen jian],加入以下代码[dai ma]:Xml代码[dai ma]<service><name>HelloWorldService</name><serviceClass>com.myeclipse.wsExample.IHelloWorldService</serviceClass><implementationClass>com.myeclipse.wsExample.HelloWorldServiceImpl</implementationClass><style>wrapped</style><use>literal</use><scope>application</scope></service><service><name>HelloWorldService</name><serviceClass>com.myeclipse.wsExample.IHelloWorldService</serviceClass><implementationClass>com.myeclipse.wsExample.HelloWorldServiceImpl</implementationClass><style>wrapped</style><use>literal</use><scope>application</scope></service>把整个项目[xiang mu]部署到tomcat 服务器[fu wu qi] 中,打开浏览[liu lan]器[liu lan qi],输入[shu ru] http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl客户[ke hu]端实现如下:Java代码[dai ma]package com.myeclipse.wsExample.client;import .MalformedURLException;import .URL;import org.codehaus.xfire.XFireFactory;import org.codehaus.xfire.client.Client;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.binding.ObjectServiceFactory;import com.myeclipse.wsExample.IHelloWorldService;public class HelloWorldClient {public static void main(String[] args) throws MalformedURLException, Exception { // TODO Auto-generated method stubService s=new ObjectServiceFactory().create(IHelloWorldService.class);XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());String url=" http://localhost:8989/HelloWorld/services/HelloWorldService ";try{IHelloWorldService hs=(IHelloWorldService) xf.create(s,url);String st=hs.example("zhangjin");System.out.print(st);}catch(Exception e){e.printStackTrace();}}}package com.myeclipse.wsExample.client;import .MalformedURLException;import .URL;import org.codehaus.xfire.XFireFactory;import org.codehaus.xfire.client.Client;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.binding.ObjectServiceFactory;import com.myeclipse.wsExample.IHelloWorldService;public class HelloWorldClient {public static void main(String[] args) throws MalformedURLException, Exception { // TODO Auto-generated method stubService s=new ObjectServiceFactory().create(IHelloWorldService.class);XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());String url=" http://localhost:8989/HelloWorld/services/HelloWorldService ";try{IHelloWorldService hs=(IHelloWorldService) xf.create(s,url);String st=hs.example("zhangjin");System.out.print(st);}catch(Exception e){e.printStackTrace();}}}这里再说点题外话,有时候我们知道一个wsdl地址[di zhi],比如想用java客户[ke hu]端引用[yin yong].net 做得webservice,使用myeclipse引用[yin yong],但是却出现无法通过验证[yan zheng]的错误[cuo wu],这时我们可以直接在类中引用[yin yong],步骤如下:Java代码[dai ma]public static void main(String[] args) throws MalformedURLException, Exception { // TODO Auto-generated method stubService s=new ObjectServiceFactory().create(IHelloWorldService.class);XFireProxyFactory xf=newXFireProxyFactory(XFireFactory.newInstance().getXFire());//远程调用[tiao yong].net开发的webserviceClient c=new Client(new URL(" /webservices/qqOnlineWebService.asmx?wsdl "));Object[] o=c.invoke("qqCheckOnline", new String[]{"531086641","591284436"});//调用[tiao yong].net本机开发的webserviceClient c1=new Client(new URL(" http://localhost/zj/Service.asmx?wsdl "));Object[] o1=c1.invoke("HelloWorld",new String[]{});}public static void main(String[] args) throws MalformedURLException, Exception {// TODO Auto-generated method stubService s=new ObjectServiceFactory().create(IHelloWorldService.class);XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire());//远程调用[tiao yong].net开发的webserviceClient c=new Client(new URL(" /webservices/qqOnlineWebService.asmx?wsdl "));Object[] o=c.invoke("qqCheckOnline", new String[]{"531086641","591284436"});//调用[tiao yong].net本机开发的webserviceClient c1=new Client(new URL(" http://localhost/zj/Service.asmx?wsdl "));Object[] o1=c1.invoke("HelloWorld",new String[]{});}三、使用axis1.4调用[tiao yong]webservice方法[fang fa]前提[qian ti]条件[tiao jian]:下载[xia zai] axis1.4包和tomcat 服务器[fu wu qi] ,并将axis文件[wen jian]夹复制到tomcat 服务器[fu wu qi] 的webapp文件[wen jian]夹中这里我就说一下最简单的方法[fang fa]:首先建立一个任意的java类(例如:HelloWorld.java),复制到axis文件[wen jian]夹下,将其扩展[kuo zhan]名改为jws,然后重新启动[qi dong][zhong xin qi dong]tomcat,在浏览[liu lan]器[liu lan qi]中输入[shu ru] http://localhost:8989/axis/HelloWorld.jws?wsdl ,就会得到一个wsdl文件[wen jian],其客户[ke hu]端调用[tiao yong]方法[fang fa]如下:Java 代码[dai ma]import javax.xml.rpc.Service;import javax.xml.rpc.ServiceException;import javax.xml.rpc.ServiceFactory;import .MalformedURLException;import .URL;import java.rmi.RemoteException;import space.QName;public class TestHelloWorld {public static void main(String[] args) throws MalformedURLException, ServiceException, RemoteException {// TODO Auto-generated method stubString wsdlUrl =" http://localhost:8989/axis/HelloWorld.jws?wsdl ";String nameSpaceUri =" http://localhost:8989/axis/HelloWorld.jws ";String serviceName = "HelloWorldService";String portName = "HelloWorld";ServiceFactory serviceFactory = ServiceFactory.newInstance();Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName));HelloWorldInterface proxy = (HelloWorldInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloWorldInterface.class);System.out.println("return value is "+proxy.getName("john") ) ;}}import javax.xml.rpc.Service;import javax.xml.rpc.ServiceException;import javax.xml.rpc.ServiceFactory;import .MalformedURLException;import .URL;import java.rmi.RemoteException;import space.QName;public class TestHelloWorld {public static void main(String[] args) throws MalformedURLException,ServiceException, RemoteException {// TODO Auto-generated method stubString wsdlUrl =" http://localhost:8989/axis/HelloWorld.jws?wsdl ";String nameSpaceUri =" http://localhost:8989/axis/HelloWorld.jws ";String serviceName = "HelloWorldService";String portName = "HelloWorld";ServiceFactory serviceFactory = ServiceFactory.newInstance();Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName));HelloWorldInterface proxy = (HelloWorldInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloWorldInterface.class);System.out.println("return value is "+proxy.getName("john") ) ;}}四、使用axis2开发webservice(这里首先感谢李宁老师)使用axis2 需要先下载[xia zai]axis2-1.4.1-bin.zipaxis2-1.4.1-war.zip/axis2/同理,也需要将axis2复制到webapp目录中在axis2中部署webservice有两种方法[fang fa],第一种是pojo方式,这种方式比较简单,但是有一些限制,例如部署的类不能加上包名第二种方式是利用xml发布webservice,这种方法[fang fa]比较灵活,不需要限制类的声明[sheng ming]下面分别说明[shuo ming]使用方法[fang fa]:1.pojo方式:在Axis2中不需要进行任何的配置[pei zhi],就可以直接将一个简单的POJO 发布成WebService。
python webservice调用Python Webservice调用是指使用Python编程语言来调用Web Service服务。
Web Service是一种基于网络的服务,它通过HTTP协议传输数据,并使用XML格式进行数据交换。
Python提供了多种方式来调用Web Service服务,包括SOAP、RESTful等。
一、SOAP方式调用Web Service服务SOAP(Simple Object Access Protocol)是一种基于XML的协议,它定义了一套标准的消息格式和传输规则。
使用SOAP方式调用Web Service服务需要以下步骤:1. 安装suds库:在Python中使用suds库来实现SOAP方式调用Web Service服务。
可以通过pip命令安装suds库。
2. 获取WSDL文件:WSDL(Web Services Description Language)是一种描述Web Service服务的语言,它定义了Web Service的接口和方法等信息。
在使用SOAP方式调用Web Service服务时需要获取WSDL文件。
3. 生成客户端代码:使用suds库的命令行工具suds.client可以根据WSDL文件生成客户端代码。
4. 调用WebService方法:通过生成的客户端代码可以直接调用WebService方法,获取返回结果。
二、RESTful方式调用Web Service服务RESTful(Representational State Transfer)是一种基于HTTP协议设计的架构风格,它将资源抽象为URI(统一资源标识符),通过HTTP请求对资源进行操作。
使用RESTful方式调用Web Service服务需要以下步骤:1. 安装requests库:在Python中使用requests库来实现RESTful方式调用Web Service服务。
可以通过pip命令安装requests库。
四种客户端调用WEBSERVICE 王凡Woshiwangfan@Wf141732@2009-08-22目录1.文档概述 (2)1.1.相关文档 (2)2.SERVER端 (3)2.1.代码 (3)2.2.SERVER端生成Wsdl (4)2.3.服务端安全性设置 (6)3.VB调用 (13)3.1.准备 (13)调用 (16)4.1.实现过程 (16)5.Axis2调用 (17)5.1.Axis2的安装 (17)5.2.Axis2调用过程 (17)6.Weblogic调用 (21)6.1.Weblogic (21)7.已解决及未解决的问题 ........................................................... 错误!未定义书签。
7.1.未解决的问题.................................................................... 错误!未定义书签。
7.2.已解决的问题.................................................................... 错误!未定义书签。
1.文档概述1.1.相关文档《Web Services开发教程.doc》2.SERVER端这个主要是用weblogic的workshop开发,用weblogic开发webservice具体可见沈辉的《Web Services开发教程.doc》,这里主要是讲客户端调用,所以服务端只作罗列。
2.1.代码在weblogic中构建webservice可以见《Web Services开发教程.doc》,用于本次测试的Server代码如下:Model:package .tsingtao.logistics.model;import java.io.Serializable;public class test implements Serializable {private static final long serialVersionUID = 1L;private String NAME;private int AGE;public void setNAME(String name){NAME=name;}public void setAGE(int age){AGE=age;}public String getNAME(){return NAME;}public int getAGE(){return AGE;}}Service代码如下:public .tsingtao.logistics.model.test[] hello(String NAME,int AGE) {.tsingtao.logistics.model.test[] t=new.tsingtao.logistics.model.test[10];for(int i=0;i<10;i++){t[i] = new .tsingtao.logistics.model.test();t[i].setNAME(NAME+i);t[i].setAGE(AGE+i);}return t;}2.2.SERVER端生成Wsdl在Server端生成的wsd如下:<?xml version='1.0' encoding='UTF-8'?><s0:definitions name="testServiceDefinitions"targetNamespace="http://cn/com/tsingtao/logistics/service" xmlns=""xmlns:s0="/wsdl/"xmlns:s1="http://cn/com/tsingtao/logistics/service"xmlns:s2="/wsdl/soap/"><s0:types><xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"targetNamespace="java:.tsingtao.logistics.model"xmlns:s0="/wsdl/"xmlns:s1="http://cn/com/tsingtao/logistics/service"xmlns:s2="/wsdl/soap/"xmlns:xs="/2001/XMLSchema"><xs:complexType name="test"><xs:sequence><xs:element minOccurs="1" name="AGE" nillable="false" type="xs:int"/><xs:element minOccurs="1" name="NAME" nillable="true" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="ArrayOftest_literal"><xs:sequence><xs:element maxOccurs="unbounded" minOccurs="0" name="test"nillable="true" type="java:test" xmlns:java="java:.tsingtao.logistics.model"/></xs:sequence></xs:complexType><xs:element name="ArrayOftest_literal" type="java:ArrayOftest_literal"xmlns:java="java:.tsingtao.logistics.model"/></xs:schema><xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://cn/com/tsingtao/logistics/service"xmlns:s0="/wsdl/"xmlns:s1="http://cn/com/tsingtao/logistics/service"xmlns:s2="/wsdl/soap/"xmlns:xs="/2001/XMLSchema"><xs:import namespace="java:.tsingtao.logistics.model"/><xs:element name="hello"><xs:complexType><xs:sequence><xs:element name="NAME" type="xs:string"/><xs:element name="AGE" type="xs:int"/></xs:sequence></xs:complexType></xs:element><xs:element name="helloResponse"><xs:complexType><xs:sequence><xs:element name="return" type="java:ArrayOftest_literal"xmlns:java="java:.tsingtao.logistics.model"/></xs:sequence></xs:complexType></xs:element></xs:schema></s0:types><s0:message name="hello"><s0:part element="s1:hello" name="parameters"/></s0:message><s0:message name="helloResponse"><s0:part element="s1:helloResponse" name="parameters"/></s0:message><s0:portType name="test"><s0:operation name="hello" parameterOrder="parameters"><s0:input message="s1:hello"/><s0:output message="s1:helloResponse"/></s0:operation></s0:portType><s0:binding name="testServiceSoapBinding" type="s1:test"><s2:binding style="document"transport="/soap/http"/><s0:operation name="hello"><s2:operation style="document"/><s0:input><s2:body parts="parameters" use="literal"/></s0:input><s0:output><s2:body parts="parameters" use="literal"/></s0:output></s0:operation></s0:binding><s0:service name="testService"><s0:port binding="s1:testServiceSoapBinding" name="testSoapPort"><s2:address location="http://localhost:7001/BSM/test"/></s0:port></s0:service></s0:definitions>2.3.服务端安全性设置这个是根据沈辉的《Web Services开发教程.doc》设置的,不作详细说明,只作罗列2.3.1.在weblogic中新建用户和用户组2.3.2.在webapp中配置安全性在web-info\web.xml中添加红色部分<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4"xmlns="/xml/ns/j2ee"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/j2ee /xml/ns/j2ee/web-app_2_4.xsd"><display-name>BSM</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><security-role><role-name>wsuser</role-name></security-role><security-constraint><web-resource-collection><web-resource-name>Secure Area</web-resource-name><url-pattern>/*</url-pattern><http-method>GET</http-method><http-method>POST</http-method></web-resource-collection><auth-constraint><role-name>wsuser</role-name></auth-constraint></security-constraint><login-config><auth-method>BASIC</auth-method><realm-name>myrealm</realm-name></login-config></web-app>在web-info\weblogic.xml中添加红色部分<?xml version="1.0" encoding="UTF-8"?><wls:weblogic-web-appxmlns:wls="/ns/weblogic/weblogic-web-app"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/javaee/xml/ns/javaee/web-app_2_5.xsd/ns/weblogic/weblogic-web-app/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd"><wls:weblogic-version>10.3</wls:weblogic-version><wls:context-root>BSM</wls:context-root><security-role-assignment><role-name>wsuser</role-name><principal-name>wsgroup</principal-name></security-role-assignment></wls:weblogic-web-app>重新发布后在浏览器中就可以看到了3.VB调用3.1.准备使用vb6.0调用webservice需要用到SOAP Toolkit,我们选用的是SOAP Toolkit3.0版本。