ホームページ  >  記事  >  バックエンド開発  >  Java を使用して WebService を呼び出す

Java を使用して WebService を呼び出す

巴扎黑
巴扎黑オリジナル
2016-12-19 16:50:361285ブラウズ

これは、Java を使用して WebService インターフェイスの C# バージョンを呼び出す例です。
C# インターフェイス:

<span style="font-size: 11px;">
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Services.Description;
[WebService(Namespace = "http://www.tangs.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () ...{
//如果使用设计的组件,请取消注释以下行 
//InitializeComponent(); 
}
 [SoapRpcMethod(Action = "http://www.tangs.com/Add", RequestNamespace = "http://www.tangs.com/T", ResponseNamespace = "http://www.tangs.com/T", Use = SoapBindingUse.Literal)]
 [WebMethod]
public int Add(int a, int b)
...{
return a + b;
 }
 [SoapRpcMethod(Action = "http://www.tangs.com/Hello", RequestNamespace = "http://www.tangs.com/T", ResponseNamespace = "http://www.tangs.com/T", Use = SoapBindingUse.Literal)]
 [WebMethod]
public String HelloWorld()
...{
return "Hello, world!";
 }
}
...</span>

この Web サービスの add メソッドと HelloWorld メソッドを呼び出す Java:

1、パラメーター メソッド: add

<span style="font-size: 11px;">
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);
 }
 }...
</span>

が実行されます。結果は次のように返されます: Result: 3

2. パラメーターなしのメソッド: HelloWorld

<span style="font-size: 11px;">
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());
 }
 }...
</span>

パラメーターなしで Web サービスを呼び出すことは、パラメーターを使用した場合と基本的に同じであることがわかりますが、パラメーターなしで呼び出す場合は、何もありません。 addParameter メソッドと setReturnType メソッドを呼び出す必要があります。
クエリクエリ結果レポートを実行して、次の内容を表示します。 Hello, world!

添付ファイルは、Web サービスが依存する JAR パッケージ内にあります


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。