最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

ASP技巧Webservice调用方式

日期:08-08    来源:中国设计秀    作者:cnwebshow.com

调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis和Soap,soap方式主要是用在websphere下Chg中国设计秀
Chg中国设计秀
axis方式调用:Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
import java.util.Date;Chg中国设计秀
Chg中国设计秀
import java.text.DateFormat;Chg中国设计秀
Chg中国设计秀
import org.apache.axis.client.Call;Chg中国设计秀
Chg中国设计秀
import org.apache.axis.client.Service;Chg中国设计秀
Chg中国设计秀
import javax.xml.namespace.QName;Chg中国设计秀
Chg中国设计秀
import java.lang.Integer;Chg中国设计秀
Chg中国设计秀
import javax.xml.rpc.ParameterMode;Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
public class caClient {Chg中国设计秀
Chg中国设计秀
public static void main(String[] args) {Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
try {Chg中国设计秀
Chg中国设计秀
String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";Chg中国设计秀
Chg中国设计秀
Service service = new Service();Chg中国设计秀
Chg中国设计秀
Call call = (Call) service.createCall();Chg中国设计秀
Chg中国设计秀
call.setTargetEndpointAddress(endpoint);Chg中国设计秀
Chg中国设计秀
call.setOperationName("addUser");Chg中国设计秀
Chg中国设计秀
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,Chg中国设计秀
Chg中国设计秀
javax.xml.rpc.ParameterMode.IN);Chg中国设计秀
Chg中国设计秀
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);Chg中国设计秀
Chg中国设计秀
call.setUseSOAPAction(true);Chg中国设计秀
Chg中国设计秀
call.setSOAPActionURI("http://www.my.com/Rpc");Chg中国设计秀
Chg中国设计秀
//Integer k = (Integer) call.invoke(new Object[] { i, j });Chg中国设计秀
Chg中国设计秀
//System.out.PRintln("result is " + k.toString() + ".");Chg中国设计秀
Chg中国设计秀
String temp = "测试人员";Chg中国设计秀
Chg中国设计秀
String result = (String)call.invoke(new Object[]{temp});Chg中国设计秀
Chg中国设计秀
System.out.println("result is "+result);Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
catch (Exception e) {Chg中国设计秀
Chg中国设计秀
System.err.println(e.toString());Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
soap方式调用Chg中国设计秀
Chg中国设计秀
调用java生成的webserviceChg中国设计秀
Chg中国设计秀
import org.apache.soap.util.xml.*;Chg中国设计秀
Chg中国设计秀
import org.apache.soap.*;Chg中国设计秀
Chg中国设计秀
import org.apache.soap.rpc.*;Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
import java.io.*;Chg中国设计秀
Chg中国设计秀
import java.net.*;Chg中国设计秀
Chg中国设计秀
import java.util.Vector;Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
public class caService{Chg中国设计秀
Chg中国设计秀
public static String getService(String user) {Chg中国设计秀
Chg中国设计秀
URL url = null;Chg中国设计秀
Chg中国设计秀
try {Chg中国设计秀
Chg中国设计秀
url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");Chg中国设计秀
Chg中国设计秀
} catch (MalformedURLException mue) {Chg中国设计秀
Chg中国设计秀
return mue.getMessage();Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
// This is the main SOAP objectChg中国设计秀
Chg中国设计秀
Call soapCall = new Call();Chg中国设计秀
Chg中国设计秀
// Use SOAP encodingChg中国设计秀
Chg中国设计秀
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);Chg中国设计秀
Chg中国设计秀
// This is the remote object we're asking for the priceChg中国设计秀
Chg中国设计秀
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");Chg中国设计秀
Chg中国设计秀
// This is the name of the method on the above objectChg中国设计秀
Chg中国设计秀
soapCall.setMethodName("getUser");Chg中国设计秀
Chg中国设计秀
// We need to send the ISBN number as an input parameter to the methodChg中国设计秀
Chg中国设计秀
Vector soapParams = new Vector();Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
// name, type, value, encoding styleChg中国设计秀
Chg中国设计秀
Parameter isbnParam = new Parameter("userName", String.class, user, null);Chg中国设计秀
Chg中国设计秀
soapParams.addElement(isbnParam);Chg中国设计秀
Chg中国设计秀
soapCall.setParams(soapParams);Chg中国设计秀
Chg中国设计秀
try {Chg中国设计秀
Chg中国设计秀
// Invoke the remote method on the objectChg中国设计秀
Chg中国设计秀
Response soapResponse = soapCall.invoke(url,"");Chg中国设计秀
Chg中国设计秀
// Check to see if there is an error, return "N/A"Chg中国设计秀
Chg中国设计秀
if (soapResponse.generatedFault()) {Chg中国设计秀
Chg中国设计秀
Fault fault = soapResponse.getFault();Chg中国设计秀
Chg中国设计秀
String f = fault.getFaultString();Chg中国设计秀
Chg中国设计秀
return f;Chg中国设计秀
Chg中国设计秀
} else {Chg中国设计秀
Chg中国设计秀
// read resultChg中国设计秀
Chg中国设计秀
Parameter soapResult = soapResponse.getReturnValue ();Chg中国设计秀
Chg中国设计秀
// get a string from the resultChg中国设计秀
Chg中国设计秀
return soapResult.getValue().toString();Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
} catch (SOAPException se) {Chg中国设计秀
Chg中国设计秀
return se.getMessage();Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
返回一维数组时Chg中国设计秀
Chg中国设计秀
Parameter soapResult = soapResponse.getReturnValue();Chg中国设计秀
Chg中国设计秀
String[] temp = (String[])soapResult.getValue();Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
调用asp.net生成的webserviceChg中国设计秀
Chg中国设计秀
private String HelloWorld(String uri, String u) {Chg中国设计秀
Chg中国设计秀
try {Chg中国设计秀
Chg中国设计秀
SOAPMappingRegistry smr = new SOAPMappingRegistry();Chg中国设计秀
Chg中国设计秀
StringDeserializer sd = new StringDeserializer();Chg中国设计秀
Chg中国设计秀
ArraySerializer arraySer = new ArraySerializer();Chg中国设计秀
Chg中国设计秀
BeanSerializer beanSer = new BeanSerializer();Chg中国设计秀
Chg中国设计秀
smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(Chg中国设计秀
Chg中国设计秀
"http://tempuri.org/", "HelloWorldResult"), String.class,Chg中国设计秀
Chg中国设计秀
null, sd);Chg中国设计秀
Chg中国设计秀
smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(Chg中国设计秀
Chg中国设计秀
"http://tempuri.org/", "temp"), String.class,Chg中国设计秀
Chg中国设计秀
beanSer, beanSer);Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
URL url = new URL(uri);Chg中国设计秀
Chg中国设计秀
Call call = new Call();Chg中国设计秀
Chg中国设计秀
call.setSOAPMappingRegistry(smr);Chg中国设计秀
Chg中国设计秀
call.setTargetObjectURI("urn:xmethods-Service1");Chg中国设计秀
Chg中国设计秀
call.setMethodName("HelloWorld");Chg中国设计秀
Chg中国设计秀
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
Vector soapParams = new Vector();Chg中国设计秀
Chg中国设计秀
soapParams.addElement(new Parameter("temp", String.class, u, null));Chg中国设计秀
Chg中国设计秀
call.setParams(soapParams);Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld");Chg中国设计秀
Chg中国设计秀
Chg中国设计秀
if (soapResponse.generatedFault()) {Chg中国设计秀
Chg中国设计秀
Fault fault = soapResponse.getFault();Chg中国设计秀
Chg中国设计秀
System.out.println(fault);Chg中国设计秀
Chg中国设计秀
} else {Chg中国设计秀
Chg中国设计秀
Parameter soapResult = soapResponse.getReturnValue();Chg中国设计秀
Chg中国设计秀
Object obj = soapResult.getValue();Chg中国设计秀
Chg中国设计秀
System.out.println("===" + obj);Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
} catch (Exception e) {Chg中国设计秀
Chg中国设计秀
e.printStackTrace();Chg中国设计秀
Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
return null;Chg中国设计秀
} Chg中国设计秀
/**Chg中国设计秀
  * 调用 C# 的webservice接口Chg中国设计秀
  * SoapRpcMethod(Action = "http://www.tangs.com/Add", Chg中国设计秀
  * RequestNamespace = "http://www.tangs.com/T", Chg中国设计秀
  * ResponseNamespace = "http://www.tangs.com/T", Chg中国设计秀
  * Use = SoapBindingUse.Literal)]Chg中国设计秀
  * Chg中国设计秀
  * */Chg中国设计秀
public static void addTest() {Chg中国设计秀
  try {Chg中国设计秀
   Integer i = 1;Chg中国设计秀
   Integer j = 2;Chg中国设计秀
Chg中国设计秀
   // WebService URLChg中国设计秀
   String service_url = "http://localhost:4079/ws/Service.asmx";Chg中国设计秀
Chg中国设计秀
   Service service = new Service();Chg中国设计秀
   Call call = (Call) service.createCall();Chg中国设计秀
   call.setTargetEndpointAddress(new java.net.URL(service_url));Chg中国设计秀
Chg中国设计秀
   // 设置要调用的方法Chg中国设计秀
   call.setOperationName(new QName("http://www.tangs.com/T", "Add"));Chg中国设计秀
Chg中国设计秀
   // 该方法需要的参数Chg中国设计秀
   call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);Chg中国设计秀
   call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);Chg中国设计秀
Chg中国设计秀
   // 方法的返回值类型Chg中国设计秀
   call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);Chg中国设计秀
Chg中国设计秀
   call.setUseSOAPAction(true);Chg中国设计秀
   call.setSOAPActionURI("http://www.tangs.com/Add");Chg中国设计秀
Chg中国设计秀
   // 调用该方法Chg中国设计秀
   Integer res = (Integer) call.invoke(new Object[] { i, j });Chg中国设计秀
Chg中国设计秀
   System.out.println("Result: " + res.toString());Chg中国设计秀
Chg中国设计秀
  } catch (Exception e) {Chg中国设计秀
   System.err.println(e);Chg中国设计秀
  }Chg中国设计秀
}Chg中国设计秀
Chg中国设计秀
/**Chg中国设计秀
  * 调用 C# 的webservice接口Chg中国设计秀
  * SoapRpcMethod(Action = "http://www.tangs.com/Add", Chg中国设计秀
  * RequestNamespace = "http://www.tangs.com/T", Chg中国设计秀
  * ResponseNamespace = "http://www.tangs.com/T", Chg中国设计秀
  * Use = SoapBindingUse.Literal)]Chg中国设计秀
  * Chg中国设计秀
  * */Chg中国设计秀
public static void helloTest() {Chg中国设计秀
  try {Chg中国设计秀
Chg中国设计秀
   String endpoint = "http://localhost:4079/ws/Service.asmx";Chg中国设计秀
   Service service = new Service();Chg中国设计秀
   Call call = (Call) service.createCall();Chg中国设计秀
   call.setTargetEndpointAddress(new java.net.URL(endpoint));Chg中国设计秀
   call.setOperationName(new QName("http://www.tangs.com/T", "HelloWorld"));Chg中国设计秀
Chg中国设计秀
   call.setUseSOAPAction(true);Chg中国设计秀
   call.setSOAPActionURI("http://www.tangs.com/Hello");Chg中国设计秀
Chg中国设计秀
   String res = (String) call.invoke(new Object[] { null });Chg中国设计秀
Chg中国设计秀
   System.out.println("Result: " + res);Chg中国设计秀
  } catch (Exception e) {Chg中国设计秀
   System.err.println(e.toString());Chg中国设计秀
  }Chg中国设计秀
}Chg中国设计秀

本文引用地址:/bc/article_46185.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明