webservice系列教學(xué)(4)-如何調(diào)用webservice(pb,java)
2024-07-21 02:21:53
供稿:網(wǎng)友
4.1 使用powerbuilder調(diào)用
適用版本8.0 需下載bulletin web services toolkit 4.1
4.2使用java調(diào)用
需要下載apache soap。下載地址:http://xml.apache.org/soap/index.html
導(dǎo)入:
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);
// 創(chuàng)建傳輸路徑和參數(shù)
soaphttpconnection st = new soaphttpconnection();
// 創(chuàng)建調(diào)用
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());
}
}
}
菜鳥學(xué)堂: