① 怎么调用java的webservice
我常去看的关于asp文章。你可以去看看,上面说的有!
http://www.ladyland.cn/developer/ASP/applications/200511/10259.html
ASP调用WEBSERVICE ----INDEX---- 1. soap请求方式 2. post请求方式 3. SHOWALLNODE函数(关于节点各属性和数据显示) --------------------- 一.SOAP请求示例 下面是一个 SOAP 请求示例。所显示的占位符需要由实际值替换。 POST /WebService1/UserSignOn.asmx HTTP/1.1 Host: 192.100.100.81 Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/LoginByAccount" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <LoginByAccount xmlns="http://tempuri.org/"> <username>string</username> <password>string</password> </LoginByAccount> </soap:Body> </soap:Envelope> 为了与WEBSERVICE交互,需要构造一个与上完全相同的SOAP请求: <% url = "http://192.100.100.81/WebService1/UserSignOn.asmx" SoapRequest="<?xml version="&CHR(34)&"1.0"&CHR(34)&" encoding="&CHR(34)&"utf-8"&CHR(34)&"?>"& _ "<soap:Envelope xmlns:xsi="&CHR(34)&"http://www.w3.org/2001/XMLSchema-instance"&CHR(34)&" "& _ "xmlns:xsd="&CHR(34)&"http://www.w3.org/2001/XMLSchema"&CHR(34)&" "& _ "xmlns:soap="&CHR(34)&"http://schemas.xmlsoap.org/soap/envelope/"&CHR(34)&">"& _ "<soap:Body>"& _ "<LoginByAccount xmlns="&CHR(34)&"http://tempuri.org/"&CHR(34)&">"& _ "<username>"&username&"</username>"& _ "<password>"&password&"</password>"& _ "</LoginByAccount>"& _ "</soap:Body>"& _ "</soap:Envelope>" Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP") xmlhttp.Open "POST",url,false xmlhttp.setRequestHeader "Content-Type", "text/xml;charset=utf-8" xmlhttp.setRequestHeader "HOST","192.100.100.81" xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest) xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/LoginByAccount" ‘一定要与WEBSERVICE的命名空间相同,否则服务会拒绝 xmlhttp.Send(SoapRequest) ‘这样就利用XMLHTTP成功发送了与SOAP示例所符的SOAP请求. ‘检测一下是否成功: Response.Write xmlhttp.Status&” ” Response.Write xmlhttp.StatusText Set xmlhttp = Nothing %> 如果成功会显示200 ok,不成功会显示 500 内部服务器错误? Connection: keep-alive . 成功后就可以利用WEBSERVICE的响应,如下: SOAP响应示例 下面是一个 SOAP 响应示例。所显示的占位符需要由实际值替换。 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <LoginByAccountResponse xmlns="http://tempuri.org/"> <LoginByAccountResult>string</LoginByAccountResult> </LoginByAccountResponse> </soap:Body> </soap:Envelope> 这是与刚才SOAP请求示例所对应的SOAP响应示例,在成功发送请求后,就可以查看该响应 : If xmlhttp.Status = 200 Then Set xmlDOC =server.CreateObject("MSXML.DOMDocument") xmlDOC.load(xmlhttp.responseXML) xmlStr = xmlDOC.xml Set xmlDOC=nothing xmlStr = Replace(xmlStr,"<","<") xmlStr = Replace(xmlStr,">",">") Response.write xmlStr Else Response.Write xmlhttp.Status&" " Response.Write xmlhttp.StatusText End if 请求正确则给出完整响应,请求不正确(如账号,密码不对)响应的内容就会信息不完整. 取出响应里的数据,如下: If xmlhttp.Status = 200 Then Set xmlDOC = server.CreateObject("MSXML.DOMDocument") xmlDOC.load(xmlhttp.responseXML) Response.Write xmlDOC.documentElement.selectNodes("//LoginByAccountResult")(0).text ‘显示节点为LoginByAccountResult的数据(有编码则要解码) Set xmlDOC = nothing Else Response.Write xmlhttp.Status&" " Response.Write xmlhttp.StatusText End if 显示某节点各个属性和数据的FUNCTION: Function showallnode(rootname,myxmlDOC)望大家不断完鄯 2005-1-9 writed by 844 if rootname<>"" then set nodeobj=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"")当前结点对像 nodeAttributelen=myxmlDOC.documentElement.selectSingleNode("//"&rootname&"").attributes.length当前结点属性数 returnstring=returnstring&"<BR>节点名称:"&rootname if nodeobj.text<>"" then returnstring=returnstring&"<BR>节点的文本:("&nodeobj.text&")" end if returnstring=returnstring&"<BR>{<BR>" if nodeAttributelen<>0 then returnstring=returnstring&"<BR>属性数有 "&nodeAttributelen&" 个,分别是:" end if for i=0 to nodeAttributelen-1 returnstring=returnstring&"<li>"&nodeobj.attributes(i).Name&": "&nodeobj.getAttribute(nodeobj.attributes(i).Name)&" </li>" next if nodeobj.childNodes.Length<>0 then if nodeobj.hasChildNodes() and lcase(nodeobj.childNodes.item(0).nodeName)<>"#text" then是否有子节点 set childnodeobj=nodeobj.childNodes childnodelen=nodeobj.childNodes.Length returnstring=returnstring&"<BR><BR>有 "&childnodelen&" 个子节点;<BR>分别是: " for i=0 to childnodelen-1 returnstring=returnstring&"<li>"&childnodeobj.item(i).nodeName&"</li>" next end if end if returnstring=returnstring&"<BR>}<BR>" response.write returnstring set nodeobj=nothing end if End Function 可以这样用: If xmlhttp.Status = 200 Then Set xmlDOC = server.CreateObject("MSXML.DOMDocument") xmlDOC.load(xmlhttp.responseXML) showallnode "LoginByAccountResponse",xmlDOC’调用SHOWALLNODE Set xmlDOC = nothing Else Response.Write xmlhttp.Status&" " Response.Write xmlhttp.StatusText End if 二.POST请求示例 HTTP POST 下面是一个 HTTP POST 请求示例。所显示的占位符需要由实际值替换。 POST /WebService1/UserSignOn.asmx/LoginByAccount HTTP/1.1 Host: 192.100.100.81 Content-Type: application/x-www-form-urlencoded Content-Length: length username=string&password=string 构造POST请求: <% url = "http://192.100.100.81/WebService1/UserSignOn.asmx/LoginByAccount" SoapRequest="username="&username&"&password="&password Set xmlhttp = server.CreateObject("Msxml2.XMLHTTP") xmlhttp.Open "POST",url,false xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"’注意 xmlhttp.setRequestHeader "HOST","192.100.100.81" xmlhttp.setRequestHeader "Content-Length",LEN(SoapRequest) xmlhttp.Send(SoapRequest) ‘这样就利用XMLHTTP成功发送了与HTTP POST示例所符的POST请求. ‘检测一下是否成功: Response.Write xmlhttp.Status&” ” Response.Write xmlhttp.StatusText Set xmlhttp = Nothing %> 如果成功会显示200 ok,不成功会显示 500 内部服务器错误? Connection: keep-alive . 成功后就可以利用WEBSERVICE的响应,如下: HTTP POST 下面是一个 HTTP POST 响应示例。所显示的占位符需要由实际值替换。 HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">string</string> 显示: If xmlhttp.Status = 200 Then Set xmlDOC = server.CreateObject("MSXML.DOMDocument") xmlDOC.load(xmlhttp.responseXML) showallnode "string",xmlDOC调用SHOWALLNODE Set xmlDOC = nothing Else Response.Write xmlhttp.Status&" " Response.Write xmlhttp.StatusText End if 以上是ASP用XMLHTTP组件发送SOAP请求,调用WEBSERVICE的方法,本人推荐在ASP环境下使用第一种方法,如果有更好的方法请联系本人mailto:[email protected] .使用HTTP GET的方式如果有中文会出问题,数据量又不大。用HTTP POST的方法感觉多此一举,其实上面的例子就是用POST的方式,只不过不是用POST的请求。用SOAP TOOLKIT要装软件,而且已没有后继版本
② java程序怎么调用webservice接口,实现发送短信功能
java程序怎么调用webservice接口,实现发送短信功能
首先我访问不到你提供的webservice
你可以直接找那个给你webservice的人对他提供的方法进行询问
不知你是什么项目框架所以不能给出具体的方案,如果是spring的话直接在配置文件中进行配置就可。首先必须获得客户端以及地址,客户端提供的是接口的定义及规范,地址是要我们进行连接的。
例如:
<bean id="XXService" class="com.xx.客户端接口" factory-bean="XXServiceFactory" factory-method="create" />
<bean id="XXServiceFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.xx.客户端接口" />
<property name="address" value="地址/>
</bean>
然后你就可以在你的业务层进行注入,这个是采用cxf的方式,当然也可以有其他方式
③ java 如何实现webservice 怎么调用接口
一、利用jdkweb服务api实现,这里使用基于SOAPmessage的Web服务
①.首先建立一个WebservicesEndPoint:packageHello;
importjavax.jws.WebService;
importjavax.jws.WebMethod;
importjavax.xml.ws.Endpoint;
@WebService
publicclassHello{
@WebMethod
publicStringhello(Stringname){
return"Hello,"+name+" ";
}
publicstaticvoidmain(String[]args){
//createandpublishanendpoint
Hellohello=newHello();
Endpointendpoint=Endpoint.publish("
,hello);
}
}
②.使用apt编译Hello.java(例:apt-d[存放编译后的文件目录]Hello.java),
会生成jaws目录
③.使用javaHello.Hello运行,然后将浏览器指向
就会出现下列显示
④.使用wsimport生成客户端使用如下:
wsimport-p.-keep
这时,会在当前目录中生成如下文件:
⑤.客户端程序:
1classHelloClient{
2publicstaticvoidmain(Stringargs[]){
3HelloServiceservice=newHelloService();
4HellohelloProxy=service.getHelloPort();
5Stringhello=helloProxy.hello("你好");
6System.out.println(hello);
7}
8}
以上方法还稍显繁琐,还有更加简单的方法
二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的利用xfire开发WebService,可以有三种方法:
1. 一种是从javabean中生成;
2.一种是从wsdl文件中生成;
3. 还有一种是自己建立webservice
步骤如下:
用myeclipse建立webservice工程,目录结构如下:首先建立webservice接口,
代码如下:
1packagecom.myeclipse.wsExample;
2//GeneratedbyMyEclipse
3
{
5
6publicStringexample(Stringmessage);
7
8}
接着实现这个借口:
1packagecom.myeclipse.wsExample;
2//GeneratedbyMyEclipse
3
{
5
6publicStringexample(Stringmessage){
7returnmessage;
8}
9
10}
修改service.xml文件,加入以下代码:
1<service>
2<name>HelloWorldService</name>
3<serviceClass>
4com.myeclipse.wsExample.IHelloWorldService
5</serviceClass>
6<implementationClass>
7com.myeclipse.wsExample.HelloWorldServiceImpl
8</implementationClass>
9<style>wrapped</style>
10<use>literal</use>
11<scope>application</scope>
12</service>
把整个项目部署到tomcat服务器中打开浏览器,输入http://localhost:8989/HelloWorld/services/HelloWorldService?wsdl,可以看到如下:
然后再展开HelloWorldService后面的wsdl可以看到:
客户端实现如下:
1packagecom.myeclipse.wsExample.client;
2
3importjava.net.MalformedURLException;
4importjava.net.URL;
5
6importorg.codehaus.xfire.XFireFactory;
7importorg.codehaus.xfire.client.Client;
8importorg.codehaus.xfire.client.XFireProxyFactory;
9importorg.codehaus.xfire.service.Service;
10importorg.codehaus.xfire.service.binding.ObjectServiceFactory;
11
12importcom.myeclipse.wsExample.IHelloWorldService;
13
14publicclassHelloWorldClient{
15publicstaticvoidmain(String[]args)throwsMalformedURLException,Exception{
16//TODOAuto-generatedmethodstub
17Services=newObjectServiceFactory().create(IHelloWorldService.class);
18XFireProxyFactoryxf=newXFireProxyFactory(XFireFactory.newInstance().getXFire());
19Stringurl="
20
21try
22{
23
24IHelloWorldServicehs=(IHelloWorldService)xf.create(s,url);
25Stringst=hs.example("zhangjin");
26System.out.print(st);
27}
28catch(Exceptione)
29{
30e.printStackTrace();
31}
32}
33
34}
有时候我们知道一个wsdl地址,比如想用java客户端引用net做得webservice,使用myeclipse引用,但是却出现无法通过验证的错误,这时我们可以直接在类中引用,步骤如下:
1.publicstaticvoidmain(String[]args)throwsMalformedURLException,Exception{
2.//TODOAuto-generatedmethodstub
④ 现在java调用webservice是用什么技术
JAVA调用WS接口现在用的比较多就是AXIS和CXF了
最早的时候是使用AXIS的比较多,因为这个是最早支持JAVA的WS接口的,像ECLIPSE里都自带了AXIS,然后因为AXIS很久没有更新了,这时候CXF慢慢进入大家眼中
CXF的接口实现起来更简单,和其它语言实现的接口互相调用的时候兼容性也很好,再加上还有REST可以更简单的访问资源,现在很多新项目都会考虑用CXF,但是有很多老项目还是用的AXIS,如果维护的话也得能看懂,所以还是可以两个都应该学学,必竟就是实现的方式有些不同而已,原理都是差不多的
关于CXF这个我之前找到一个哥们写的一些很不错的笔记,如果有兴趣的话你可以去参考参考 http://my.oschina.net/huangyong/blog/294324
⑤ 如何调用别人提供的webservice接口
在项目中选择【控制台应用程序】,点击项目右键,选择添加->服务引用。在地址栏中输入WebServie链接地址后回车,点击确定后在代码中就可以看到添加的服务应用了,详细步骤:
1、首先打开VS2013,选择文件->新建->项目。
⑥ java调用webservice接口具体怎么调用
Java调用WebService可以直接使用Apache提供的axis.jar自己编写代码,或者利用Eclipse自动生成WebService Client代码,利用其中的Proxy类进行调用。理论上是一样的,只不过用Eclipse自动生成代码省事些。
1、编写代码方式:
package com.yun.test;
import java.rmi.RemoteException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.SOAPHeaderElement;
import com.cezanne.golden.user.Exception;
import com.cezanne.golden.user.UserManagerServiceProxy;
import javax.xml.namespace.QName;
import java.net.MalformedURLException;
import javax.xml.rpc.ServiceException;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPException;
public class testWebService {
public static String getResult() throws ServiceException, MalformedURLException, RemoteException, SOAPException
{
//标识Web Service的具体路径
String endpoint = "WebService服务地址";
// 创建 Service实例
Service service = new Service();
// 通过Service实例创建Call的实例
Call call = (Call) service.createCall();
//将Web Service的服务路径加入到call实例之中.
call.setTargetEndpointAddress( new java.net.URL(endpoint) );//为Call设置服务的位置
// 由于需要认证,故需要设置调用的SOAP头信息。
Name headerName = new PrefixedQName( new QName("发布的wsdl里的targetNamespace里的url", "string_itemName") );
org.apache.axis.message.SOAPHeaderElement header = new SOAPHeaderElement(headerName);
header.addTextNode( "blablabla" );
call.addHeader(header);
// SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement("发布的wsdl里的targetNamespace里的url", "SoapHeader");
// soapHeaderElement.setNamespaceURI("发布的wsdl里的targetNamespace里的url");
// try
// {
// soapHeaderElement.addChildElement("string_itemName").setValue("blablabla");
// }
// catch (SOAPException e)
// {
// e.printStackTrace();
// }
// call.addHeader(soapHeaderElement);
//调用Web Service的方法
org.apache.axis.description.OperationDesc oper;
org.apache.axis.description.ParameterDesc param;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("opName");
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg0"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg1"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
param = new org.apache.axis.description.ParameterDesc(new javax.xml.namespace.QName("", "arg2"), org.apache.axis.description.ParameterDesc.IN, new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"), java.lang.String.class, false, false);
param.setOmittable(true);
oper.addParameter(param);
⑦ java 怎样调用.net 写的webservice
一. 使用axis1.x调用webservice方法
Axis的最常用版本:1.4和2.0版本。以下为1.4版本
核心代码:
// webserviceURL
service_url = "http://vip.cxcod.com/PodApi/GetPodStr.asmx?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(service_url));
// 设置要调用的方法
// http://intelink.net/是wsdl中definitions根节点的targetNamespace属性值
call.setOperationName(new QName("http://intelink.net/","GetStrByJobno"));
// 该方法需要的参数
call.addParameter("CustNo",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("passwd",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("Jobno",
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
// 方法的返回值类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
// call.setUseSOAPAction(true); //call.setSOAPActionURI("http://intelink.net/GetStrByJobno");
// 调用该方法, new Object[] { CustNo, passwd, Jobno}为参数列表
String xmlStr = call.invoke(new Object[] { CustNo, passwd, Jobno}).toString();
} catch (Exception e) {
e.printStackTrace();
}
JAVA用这种方式调用webservice,需要注意的地方:
1. 服务器未能识别 HTTP 标头 SOAPAction 的值:
症状一:
Web Service + ASP.NET 应用程序部署到服务器默认目录中,在IE中用http://<服务器地址>/<程序目录名>/<默认启动页面名>发生“服务器未能识别 HTTP 标头 SOAPAction 的值”错误。
症状二:
在Java平台上调用.NET Web Service的服务时,出现"服务器未能识别 HTTP 标头 SOAPAction 的值"。
症状三:
在Java平台下调用.NET WEB Service,出现数据时有时无。
解决对策:
给.NET的WebService类(即.asmx文件下的类)添加属性[SoapDocumentService(RoutingStyle=SoapServiceRoutingStyle.RequestElement)]
小知识:
什么是SoapAction?它在WSDL中有何作用?
SOAPAction HTTP request header被用来标识SOAP HTTP请求的目的地,其值是个URI地址。SOAP发送并不限制格式、URI特征或其必须可解析,那么在这种情况下,发送一个HTTP SOAP请求时,其HTTP客户端必须使用/指明SOAPAction HTTP request header。
SOAPAction header的内容可以被用在服务端,诸如:防火墙适当的过滤基于HTTP的SOAP请求消息等场景。SOAPAction header的值为空串("")表示SOAP消息的目的地由HTTP请求的URI标识;无值则表示没有指定这条消息的目的地。
本人补充:
在.NET环境调用.NET WebService出现 “SOAPAction 值在 XML Web services 的所有方法中不唯一的错误”,也可以通过此法解决。
2. 为了Java能够调用WebService的方法,所以。NETP写的WebServiced的每个方法都要声明为Rpc方法,即添加"[SoapRpcMethod.....]".
例如:[WebMethod]
[SoapRpcMethod(Use=SoapBindingUse.Literal,Action= http://tempuri.org/HelloWorld", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]
3. 对返回值、参数的处理上:
应尽量将webservice方法的返回值、参数都写成字符串(String)不要使用复杂对象类型,这样便于在网络上传输。避免了复杂对象类型的不易转换问题。。。对于返回类型是字符串数组型的,可以设置返回类型为org.apache.axis.encoding.XMLType.SOAP_VECTOR或java.lang.String[].class.
二.利用xfire调用WebService
XFire是新一代的Java Web服务引擎,XFire使得在JavaEE应用中发布Web服务变得轻而易举。和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地和Spring集成,它使得Java开发人员终于可以获得和.Net开发人员一样的开发效率。
核心代码:
Service service = new ObjectServiceFactory().create(IWebservice.class);
XFireProxyFactory factory =
new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String url= "http://localhost:8080/webservices/services/webservices";
IWebservice iw = (IWebservice) factory.create(service, url);
List list=iw.getTest();
出处:http://liyuandong.iteye.com/blog/567836
⑧ 怎么用Java通过wsdl地址调用WebService求代码
我觉得这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
2,直接SOAP调用远程的webservice
这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class caService{
public static String getService(String user) {
URL url = null;
try {
url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {
return se.getMessage();
}
}
}
⑨ Java调用webservice和postmain调用的区别
区别是WebService可以有Get、Post、Soap、Document四种方式调用。
我们可以把webservice看做是web服务器上的一个应用,web服务器是webservice的一个容器。通过wximport生成代码。通过客户端编程方式。
通过URLConnection方式调用。
⑩ java如何调用webservice接口
Java通过WSDL文件来调用webservice直接调用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}