首页 > 开发 > Java > 正文

webservice系列教学(4)-如何调用webservice(pb,java)

2024-07-21 02:21:53
字体:
来源:转载
供稿:网友
4.1 使用powerbuilder调用
    适用版本8.0 需下载bulletin web services toolkit 4.1
4.2使用java调用
需要下载apache soap。下载地址:http://xml.apache.org/soap/index.html
导入:
import org.apache.soap.*;
import org.apache.soap.rpc.*;
例程:
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.soaphttpconnection;

public class testclient {

    public static void main(string[] args) throws exception {

        url url = new url ("http://192.168.0.4/yundan/service1.wsdl");
//改成你的地址
        soapmappingregistry smr = new soapmappingregistry ();
        stringdeserializer sd = new stringdeserializer ();
        smr.maptypes (constants.ns_uri_soap_enc, new qname ("", "result"), null, null, sd);

        // 创建传输路径和参数
        soaphttpconnection st = new soaphttpconnection();

        // 创建调用
        call call = new call ();
        call.setsoaptransport(st);
        call.setsoapmappingregistry (smr);

        call.settargetobjecturi ("http://tempuri.org/message/");
        call.setmethodname("addnumbers");
        call.setencodingstyleuri ("http://schemas.xmlsoap.org/soap/encoding/");

        vector params = new vector();
        params.addelement(new parameter("numberone", double.class, "10", null));
        params.addelement(new parameter("numbertwo", double.class, "25", null));
        call.setparams(params);

        response resp = null;

        try {
          resp = call.invoke (url, "http://tempuri.org/action/hello2.addnumbers");
        }
        catch (soapexception e) {
        system.err.println("caught soapexception (" + e.getfaultcode () + "): " + e.getmessage ());
        return;
        }

        // 检查返回值
        if (resp != null && !resp.generatedfault()) {
        parameter ret = resp.getreturnvalue();
        object value = ret.getvalue();

        system.out.println ("answer--> " + value);
        }
        else {
            fault fault = resp.getfault ();
            system.err.println ("generated fault: ");
            system.out.println (" fault code = " + fault.getfaultcode());
            system.out.println (" fault string = " + fault.getfaultstring());
        }
    }
}
菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表