`

call webservice

阅读更多
web services是用delphi写的,接口如下:

interface

uses InvokeRegistry, Types, XSBuiltIns;

type

  { Invokable interfaces must derive from IInvokable }
  IQQService = interface(IInvokable)
  ['{9604D694-3FFF-4D64-9041-0EE9BE9C166C}']

    { Methods of Invokable interface must not use the default }
    { calling convention; stdcall is recommended }
    function ListAccount(var QQList : Olevariant):boolean; stdcall;//列出可供申请的QQ号码
    function RequestQQ():String;stdcall; //随机产生一个QQ号码
    function GetQQPassword(QQ:String):String;stdcall; //取得相应QQ号码的密码
  end;

implementation

initialization
  { Invokable interfaces must be registered }
  InvRegistry.RegisterInterface(TypeInfo(IQQService));

end.


调用web services的代码:
package sunflowerbbs.oicp.net;

import java.net.URL;
import java.util.Vector;

import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;

public class Soap {

	final static String SOAPURL = "http://sunflowerbbs.oicp.net:9015/UniversalService/QQService.dll/soap/IQQService";

	public String doSOAPRequest(String _strURI, String _strMethodName,
			String _strName, String _strValue) throws Exception {
		// 创建一个远程调用
		Call call = new Call();
		// 设置远程对象的URI
		call.setTargetObjectURI("urn:" + _strURI);
		// 设置调用的方法名
		call.setMethodName(_strMethodName);
		// 设置编码风格
		call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
		// 设置方法调用的参数
		if (_strName != null) {
			Vector params = new Vector();
			params.addElement(new Parameter(_strName, String.class, _strValue,
					null));
			call.setParams(params);
		}
		// 发送RPC请求
		Response resp = call.invoke(new URL(SOAPURL), "");
		if (resp.generatedFault()) {
			// 远程调用出错处理
			Fault fault = resp.getFault();
			System.out.println("调用失败!");
			System.out.println("错误代号:" + fault.getFaultCode());
			System.out.println("错误信息:" + fault.getFaultString());
			return "调用失败!";
		} else {
			// 调用成功,获取返回值
			Parameter result = resp.getReturnValue();
			return result.getValue().toString();
		}
	}

	public static void main(String[] args) {
		System.out.println("SOAP调用测试开始。。。");
		try {
			Soap mysoap = new Soap();
			// 调用远程的SOAP服务
			String qq = mysoap.doSOAPRequest("QQServiceIntf-IQQService",
					"RequestQQ", null, null);
			String password = mysoap.doSOAPRequest("QQServiceIntf-IQQService",
					"GetQQPassword", "QQ", qq);
			System.out
					.println(String.format("QQ=%s,Password=%s", qq, password));
		} catch (Exception e) {
			System.out.println("调用Soap异常!");
			e.printStackTrace();
		}
		System.out.println("SOAP调用测试结束。。。");
	}

}

再看看wsdl:
<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="IQQServiceservice" targetNamespace="http://tempuri.org/" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/">
  <message name="ListAccount0Request">
    <part name="QQList" type="xs:anyType"/>
  </message>
  <message name="ListAccount0Response">
    <part name="QQList" type="xs:anyType"/>
    <part name="return" type="xs:boolean"/>
  </message>
  <message name="RequestQQ1Request"/>
  <message name="RequestQQ1Response">
    <part name="return" type="xs:string"/>
  </message>
  <message name="GetQQPassword2Request">
    <part name="QQ" type="xs:string"/>
  </message>
  <message name="GetQQPassword2Response">
    <part name="return" type="xs:string"/>
  </message>
  <portType name="IQQService">
    <operation name="ListAccount">
      <input message="tns:ListAccount0Request"/>
      <output message="tns:ListAccount0Response"/>
    </operation>
    <operation name="RequestQQ">
      <input message="tns:RequestQQ1Request"/>
      <output message="tns:RequestQQ1Response"/>
    </operation>
    <operation name="GetQQPassword">
      <input message="tns:GetQQPassword2Request"/>
      <output message="tns:GetQQPassword2Response"/>
    </operation>
  </portType>
  <binding name="IQQServicebinding" type="tns:IQQService">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="ListAccount">
      <soap:operation soapAction="urn:QQServiceIntf-IQQService#ListAccount" style="rpc"/>
      <input message="tns:ListAccount0Request">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:QQServiceIntf-IQQService"/>
      </input>
      <output message="tns:ListAccount0Response">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:QQServiceIntf-IQQService"/>
      </output>
    </operation>
    <operation name="RequestQQ">
      <soap:operation soapAction="urn:QQServiceIntf-IQQService#RequestQQ" style="rpc"/>
      <input message="tns:RequestQQ1Request">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:QQServiceIntf-IQQService"/>
      </input>
      <output message="tns:RequestQQ1Response">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:QQServiceIntf-IQQService"/>
      </output>
    </operation>
    <operation name="GetQQPassword">
      <soap:operation soapAction="urn:QQServiceIntf-IQQService#GetQQPassword" style="rpc"/>
      <input message="tns:GetQQPassword2Request">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:QQServiceIntf-IQQService"/>
      </input>
      <output message="tns:GetQQPassword2Response">
        <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:QQServiceIntf-IQQService"/>
      </output>
    </operation>
  </binding>
  <service name="IQQServiceservice">
    <port name="IQQServicePort" binding="tns:IQQServicebinding">
      <soap:address location="http://sunflowerbbs.oicp.net:9015/UniversalService/QQService.dll/soap/IQQService"/>
    </port>
  </service>
</definitions>


注意:
<operation name="GetQQPassword"> 
<soap:operation soapAction="urn:QQServiceIntf-IQQService#GetQQPassword" style="rpc"/> 
<input message="tns:GetQQPassword2Request"> 
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:QQServiceIntf-IQQService"/> 
</input> 
<output message="tns:GetQQPassword2Response"> 
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:QQServiceIntf-IQQService"/> 
</output> 
</operation>

它标识了命名空间下的函数名,输入参数,输出参数.

注意
<soap:address location="http://sunflowerbbs.oicp.net:9015/UniversalService/QQService.dll/soap/IQQService"/> 

它标识了soap调用的地址.

下面附注源码(包含必须的xerces.jar,activation.jar,soap.jar,mail.jar).
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics