国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Webservice調用方式

2019-11-17 03:55:32
字體:
來源:轉載
供稿:網友
調用webservice,可以首先根據wsdl文件生成客戶端,或者直接根據地址調用,下面討論直接調用地址的兩種不同方式:axis和Soap,soap方式主要是用在websphere下

axis方式調用:


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";

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");

//Integer k = (Integer) call.invoke(new Object[] { i, j });

//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

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();

}

}

}

返回一維數組時

Parameter soapResult = soapResponse.getReturnValue();

String[] temp = (String[])soapResult.getValue();


調用asp.net生成的webservice

private String HelloWorld(String uri, String u) {

try {

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"), String.class,

null, sd);

smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(

"http://tempuri.org/", "temp"), String.class,

beanSer, beanSer);


URL url = new URL(uri);

Call call = new Call();

call.setSOAPMappingRegistry(smr);

call.setTargetObjectURI("urn:xmethods-Service1");

call.setMethodName("HelloWorld");

call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);


Vector soapParams = new Vector();

soapParams.addElement(new Parameter("temp", String.class, u, null));

call.setParams(soapParams);


Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld");


if (soapResponse.generatedFault()) {

Fault fault = soapResponse.getFault();

System.out.println(fault);

} else {

Parameter soapResult = soapResponse.getReturnValue();

Object obj = soapResult.getValue();

System.out.println("===" + obj);

}

} catch (Exception e) {

e.printStackTrace();

}

return null;
}
/**
  * 調用 C# 的webservice接口
  * SoapRpcMethod(Action = "http://www.tangs.com/Add",
  * RequestNamespace = "http://www.tangs.com/T",
  * ResponseNamespace = "http://www.tangs.com/T",
  * Use = SoapBindingUse.Literal)]
  *
  * */
public static void addTest() {
  try {
   Integer i = 1;
   Integer j = 2;

   // WebService URL
   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", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);

   // 方法的返回值類型
   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());

  } catch (Exception e) {
   System.err.println(e);
  }
}

/**
  * 調用 C# 的webservice接口
  * SoapRpcMethod(Action = "http://www.tangs.com/Add",
  * RequestNamespace = "http://www.tangs.com/T",
  * ResponseNamespace = "http://www.tangs.com/T",
  * Use = SoapBindingUse.Literal)]
  *
  * */
public static void helloTest() {
  try {

   String endpoint = "http://localhost:4079/ws/Service.asmx";
   Service service = new Service();
   Call call = (Call) service.createCall();
   call.setTargetEndpointAddress(new java.net.URL(endpoint));
   call.setOperationName(new QName("http://www.tangs.com/T", "HelloWorld"));

   call.setUseSOAPAction(true);
   call.setSOAPActionURI("http://www.tangs.com/Hello");

   String res = (String) call.invoke(new Object[] { null });

   System.out.println("Result: " + res);
  } catch (Exception e) {
   System.err.println(e.toString());
  }
}





本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/jimmy292/archive/2009/12/28/5089335.aspx
上一篇:Create ajax

下一篇: Java連接SQL Server 2000

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇远县| 始兴县| 晋城| 金乡县| 五常市| 德州市| 洪湖市| 辽源市| 页游| 宣武区| 台前县| 丘北县| 阿巴嘎旗| 清苑县| 平果县| 丹棱县| 乌什县| 正蓝旗| 文水县| 昌宁县| 阿拉善左旗| 忻城县| 汾西县| 古丈县| 翁牛特旗| 团风县| 乌鲁木齐市| 安庆市| 资源县| 贡觉县| 沙雅县| 和平县| 涟源市| 绥化市| 临城县| 安龙县| 宣威市| 沙田区| 门头沟区| 龙江县| 海伦市|