转自:[url] http://blog.csdn.net/baiboy4493/archive/2009/03/13/3987526.aspx [/url]?
调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis和Soap,soap方式主要是用在websphere下?
axis方式调用(Java代码):?
- 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?{??
- static?void?main(String[]?args)?{??
- try?{??
- String?endpoint?=?"http://localhost:8080/ca3/services/caSynrochnized?wsdl";??
- Service?service?=?new?Service();??
- Call?call?=?(Call)?service.createCall();??
- call.setTargetEndpointAddress(endpoint);??
- call.setOperationName("addUser");??
- call.addParameter("userName",?org.apache.axis.encoding.XMLType.XSD_DATE,??
- javax.xml.rpc.ParameterMode.IN);??
- call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);??
- call.setUseSOAPAction(true);??
- call.setSOAPActionURI("http://www.my.com/Rpc");??
- ??
- //System.out.println("result?is?"?+?k.toString()?+?".");??
- String?temp?=?"测试人员";??
- String?result?=?(String)call.invoke(new?Object[]{temp});??
- System.out.println("result?is?"+result);??
- }??
- catch?(Exception?e)?{??
- System.err.println(e.toString());??
- }??
soap方式调用?
调用java生成的webservice (Java代码 )
- 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{??
- 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();??
- }??
- ??
- 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);??
- //?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代码)
Parameter?soapResult?=?soapResponse.getReturnValue();??
String[]?temp?=?(String[])soapResult.getValue();??
调用ASP.Net生成的webservice (Java代码)
private?String?HelloWorld(String?uri,?String?u)?{??
SOAPMappingRegistry?smr?=?new?SOAPMappingRegistry();??
StringDeserializer?sd?=?new?StringDeserializer();??
ArraySerializer?arraySer?=?new?ArraySerializer();??
BeanSerializer?beanSer?=?new?BeanSerializer();??
smr.mapTypes(Constants.NS_URI_SOAP_ENC,?new?QName(??
"http://tempuri.org/",?"HelloWorldResult"),??
null,?sd);??
"temp"),250); line-height:18px"> beanSer,?beanSer);??
new?URL(uri);??
Call?call?=? call.setSOAPMappingRegistry(smr);??
call.setTargetObjectURI("urn:xmethods-Service1");??
call.setMethodName("HelloWorld");??
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);??
soapParams.addElement(new?Parameter("temp",?u,85); font-weight:bold">null));??
call.setParams(soapParams);??
Response?soapResponse?=?call.invoke(url,"http://tempuri.org/HelloWorld");??
System.out.println(fault);??
Parameter?soapResult?=?soapResponse.getReturnValue();??
Object?obj?=?soapResult.getValue();??
System.out.println("==="?+?obj);??
catch?(Exception?e)?{??
e.printStackTrace();??
return? }???
/**?
??*?调用?C#?的webservice接口?
??*?SoapRpcMethod(Action?=?"http://www.tangs.com/Add",??
??*?RequestNamespace?=?"http://www.tangs.com/T",0)">??*?ResponseNamespace?=?"http://www.tangs.com/T",0)">??*?Use?=?SoapBindingUse.Literal)]?
??*??
??*?*/??
?static?void?addTest()?{??
?? ???Integer?i?=?1;??
???Integer?j?=?2;??
?????
???String?service_url?=?"http://localhost:4079/ws/Service.asmx";??
???Service?service?=?new?Service();??
???Call?call?=?(Call)?service.createCall();??
???call.setTargetEndpointAddress(new?java.net.URL(service_url));??
//?设置要调用的方法??
???call.setOperationName(new?QName("http://www.tangs.com/T",?"Add"));??
//?该方法需要的参数??
???call.addParameter("a",?org.apache.axis.encoding.XMLType.XSD_INT,?javax.xml.rpc.ParameterMode.IN);??
???call.addParameter("b",0)">//?方法的返回值类型??
???call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);??
???call.setUseSOAPAction(true);??
???call.setSOAPActionURI("http://www.tangs.com/Add");??
//?调用该方法??
???Integer?res?=?(Integer)?call.invoke(new?Object[]?{?i,?j?});??
???System.out.println("Result:?"?+?res.toString());??
??}? ???System.err.println(e);??
??}??
?}??
??helloTest()?{??
???String?endpoint?=?"http://localhost:4079/ws/Service.asmx";??
new?java.net.URL(endpoint));??
"HelloWorld"));??
???call.setSOAPActionURI("http://www.tangs.com/Hello");??
???String?res?=?(String)?call.invoke(new?Object[]?{?null?});??
???System.out.println("Result:?"?+?res);??
???System.err.println(e.toString());??
?} ?