日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java调用net webservice_java调用.net的webservice

發布時間:2024/9/3 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java调用net webservice_java调用.net的webservice 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.參考文獻

二.概述

前面寫了一篇博客eclipse+webservice?是在java環境下進行的。考慮到webservice的跨系統,跨語言,跨網絡的特性,下面寫了一個實例來測試其跨語言的的特性。

首先是用asp.net開發一個webservice,然后再java中創建客戶端來調用這個service。

三.實例

(1)打開visual studio 2010,新建項目,如下圖所示:

新建的項目結果如下圖所示:

(2)在Service1.asmx.cs中添加服務方法,代碼如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Web;

usingSystem.Web.Services;

namespaceAspWebService1

{

///?

///?Service1?的摘要說明

///?

[WebService(Namespace?="http://erplab.sjtu.edu/")]

[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

//?若要允許使用?ASP.NET?AJAX?從腳本中調用此?Web?服務,請取消對下行的注釋。

//?[System.Web.Script.Services.ScriptService]

publicclassService1?:?System.Web.Services.WebService

{

[WebMethod]

publicstringHelloWorld()

{

return"Hello?World";

}

[WebMethod]

publicstringsayHelloToPersonNew(String?name)

{

if(name?==null)

{

name?="nobody";

}

return"hello,"+?name;

}

[WebMethod]

publicdoublecount(doublenumber,doubleprice,doublediscount)

{

returnnumber?*?price?*?discount;

}

[WebMethod]

publicfloatgetFloat(floatx)

{

returnx;

}

//加法

[WebMethod]

publicfloatplus(floatx,floaty)

{

returnx?+?y;

}

//減法

[WebMethod]

publicfloatminus(floatx,floaty)

{

returnx?-?y;

}

//乘法

[WebMethod]

publicfloatmultiply(floatx,floaty)

{

returnx?*?y;

}

//除法

[WebMethod]

publicfloatdivide(floatx,floaty)

{

if(y?!=?0)

{

returnx?/?y;

}

else

return-1;

}

}

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

namespace AspWebService1

{

///

/// Service1 的摘要說明

///

[WebService(Namespace = "http://erplab.sjtu.edu/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。

// [System.Web.Script.Services.ScriptService]

public class Service1 : System.Web.Services.WebService

{

[WebMethod]

public string HelloWorld()

{

return "Hello World";

}

[WebMethod]

public string sayHelloToPersonNew(String name)

{

if (name == null)

{

name = "nobody";

}

return "hello," + name;

}

[WebMethod]

public double count(double number, double price, double discount)

{

return number * price * discount;

}

[WebMethod]

public float getFloat(float x)

{

return x;

}

//加法

[WebMethod]

public float plus(float x, float y)

{

return x + y;

}

//減法

[WebMethod]

public float minus(float x, float y)

{

return x - y;

}

//乘法

[WebMethod]

public float multiply(float x, float y)

{

return x * y;

}

//除法

[WebMethod]

public float divide(float x, float y)

{

if (y != 0)

{

return x / y;

}

else

return -1;

}

}

}

(3)發布服務,按CTRL+F5運行項目,即可打開服務首頁:http://localhost:5329/Service1.asmx,如下圖所示:

上圖中顯示的就是我們在Service1.asmx.cs文件中定義的服務方法。點擊“服務說明”可以查看webservice的wsdl文件。

(4)編寫java客戶端來測試webservice,java程序如下所示:

package?edu.sjtu.erplab.aspwebservice;

import?javax.xml.namespace.QName;

import?javax.xml.rpc.ParameterMode;

import?org.apache.axis.client.Call;

import?org.apache.axis.client.Service;

import?org.apache.axis.encoding.XMLType;

publicclassAspWebServiceTestClient1?{

publicstaticvoidmain(String[]?args)?throws?Exception?{

//?定義方法

String?method?="HelloWorld";

String?methodPlus?="plus";

String?methodMinus?="minus";

String?methodMultiply?="multiply";

String?methodDivide?="divide";

String?methodSayTo?="sayHelloToPersonNew";

//?定義服務

Service?service?=newService();

//?測試1:調用HelloWorld方法,方法沒有參數

Call?call?=?(Call)?service.createCall();

call.setTargetEndpointAddress(newjava.net.URL(

"http://localhost:5329/Service1.asmx"));

call.setUseSOAPAction(true);

//?第一種設置返回值類型為String的方法

call.setReturnType(XMLType.SOAP_STRING);

call.setOperationName(newQName("http://erplab.sjtu.edu/",?method));

call.setSOAPActionURI("http://erplab.sjtu.edu/HelloWorld");

String?retVal1?=?(String)?call.invoke(newObject[]?{});

System.out.println(retVal1);

//?測試2:?調用sayHelloToPersonNew方法,方法有一個參數:name。sayHelloToPersonNew(String?name)

Call?call2?=?(Call)?service.createCall();

call2.setTargetEndpointAddress(newjava.net.URL(

"http://localhost:5329/Service1.asmx"));

call2.setUseSOAPAction(true);

call2.setReturnType(newQName("http://www.w3.org/2001/XMLSchema",

"string"));

//?第二種設置返回值類型為String的方法

call2.setOperationName(newQName("http://erplab.sjtu.edu/",?methodSayTo));

call2.setSOAPActionURI("http://erplab.sjtu.edu/sayHelloToPersonNew");

call2.addParameter(newQName("http://erplab.sjtu.edu/","name"),//?這里的name對應參數名稱

XMLType.XSD_STRING,?ParameterMode.IN);

String?retVal2?=?(String)?call2

.invoke(newObject[]?{"asp?webservice"});

System.out.println(retVal2);

//?測試3:?調用sgetFloat方法,方法有一個參數:x,為float類型

Call?call3?=?(Call)?service.createCall();

call3.setTargetEndpointAddress(newjava.net.URL(

"http://localhost:5329/Service1.asmx"));

call3.setUseSOAPAction(true);

call3.setEncodingStyle(null);//?必須有,否為會系統報錯。最關鍵的語句。決定生成xmlns的中soap的命名空間

//?第一種設置返回值類型為Float類型的方法

call3.setReturnType(org.apache.axis.encoding.XMLType.XSD_FLOAT);

call3.setOperationName(newQName("http://erplab.sjtu.edu/","getFloat"));

call3.setSOAPActionURI("http://erplab.sjtu.edu/getFloat");

call3.addParameter(newQName("http://erplab.sjtu.edu/","x"),//?這里的x對應參數名稱

XMLType.XSD_FLOAT,?ParameterMode.INOUT);

Float?retVal3?=?(Float)?call3.invoke(newObject[]?{?123?});

System.out.println(retVal3);

//?測試4:?調用plus方法,方法有兩個參數:x,y。plus(float?x,?float?y)

Call?call4?=?(Call)?service.createCall();

call4.setTargetEndpointAddress(newjava.net.URL(

"http://localhost:5329/Service1.asmx"));

call4.setUseSOAPAction(true);

call4.setEncodingStyle(null);

//?第二種設置返回值類型為Float類型的方法

call4.setReturnType(newQName("http://www.w3.org/2001/XMLSchema",

"float"));

call4.setOperationName(newQName("http://erplab.sjtu.edu/",?methodPlus));//?加法

call4.setSOAPActionURI("http://erplab.sjtu.edu/plus");

call4.addParameter(newQName("http://erplab.sjtu.edu/","x"),//?參數x

org.apache.axis.encoding.XMLType.XSD_FLOAT,?ParameterMode.IN);

call4.addParameter(newQName("http://erplab.sjtu.edu/","y"),//?參數y

XMLType.XSD_FLOAT,?ParameterMode.IN);

Float?retVal4?=?(Float)?call4.invoke(newObject[]?{?5,?6?});

System.out.println(retVal4);

}

}

package edu.sjtu.erplab.aspwebservice;

import javax.xml.namespace.QName;

import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.encoding.XMLType;

public class AspWebServiceTestClient1 {

public static void main(String[] args) throws Exception {

// 定義方法

String method = "HelloWorld";

String methodPlus = "plus";

String methodMinus = "minus";

String methodMultiply = "multiply";

String methodDivide = "divide";

String methodSayTo = "sayHelloToPersonNew";

// 定義服務

Service service = new Service();

// 測試1:調用HelloWorld方法,方法沒有參數

Call call = (Call) service.createCall();

call.setTargetEndpointAddress(new java.net.URL(

"http://localhost:5329/Service1.asmx"));

call.setUseSOAPAction(true);

// 第一種設置返回值類型為String的方法

call.setReturnType(XMLType.SOAP_STRING);

call.setOperationName(new QName("http://erplab.sjtu.edu/", method));

call.setSOAPActionURI("http://erplab.sjtu.edu/HelloWorld");

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

System.out.println(retVal1);

// 測試2: 調用sayHelloToPersonNew方法,方法有一個參數:name。sayHelloToPersonNew(String name)

Call call2 = (Call) service.createCall();

call2.setTargetEndpointAddress(new java.net.URL(

"http://localhost:5329/Service1.asmx"));

call2.setUseSOAPAction(true);

call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",

"string"));

// 第二種設置返回值類型為String的方法

call2.setOperationName(new QName("http://erplab.sjtu.edu/", methodSayTo));

call2.setSOAPActionURI("http://erplab.sjtu.edu/sayHelloToPersonNew");

call2.addParameter(new QName("http://erplab.sjtu.edu/", "name"),// 這里的name對應參數名稱

XMLType.XSD_STRING, ParameterMode.IN);

String retVal2 = (String) call2

.invoke(new Object[] { "asp webservice" });

System.out.println(retVal2);

// 測試3: 調用sgetFloat方法,方法有一個參數:x,為float類型

Call call3 = (Call) service.createCall();

call3.setTargetEndpointAddress(new java.net.URL(

"http://localhost:5329/Service1.asmx"));

call3.setUseSOAPAction(true);

call3.setEncodingStyle(null);// 必須有,否為會系統報錯。最關鍵的語句。決定生成xmlns的中soap的命名空間

// 第一種設置返回值類型為Float類型的方法

call3.setReturnType(org.apache.axis.encoding.XMLType.XSD_FLOAT);

call3.setOperationName(new QName("http://erplab.sjtu.edu/", "getFloat"));

call3.setSOAPActionURI("http://erplab.sjtu.edu/getFloat");

call3.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 這里的x對應參數名稱

XMLType.XSD_FLOAT, ParameterMode.INOUT);

Float retVal3 = (Float) call3.invoke(new Object[] { 123 });

System.out.println(retVal3);

// 測試4: 調用plus方法,方法有兩個參數:x,y。plus(float x, float y)

Call call4 = (Call) service.createCall();

call4.setTargetEndpointAddress(new java.net.URL(

"http://localhost:5329/Service1.asmx"));

call4.setUseSOAPAction(true);

call4.setEncodingStyle(null);

// 第二種設置返回值類型為Float類型的方法

call4.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",

"float"));

call4.setOperationName(new QName("http://erplab.sjtu.edu/", methodPlus));// 加法

call4.setSOAPActionURI("http://erplab.sjtu.edu/plus");

call4.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 參數x

org.apache.axis.encoding.XMLType.XSD_FLOAT, ParameterMode.IN);

call4.addParameter(new QName("http://erplab.sjtu.edu/", "y"),// 參數y

XMLType.XSD_FLOAT, ParameterMode.IN);

Float retVal4 = (Float) call4.invoke(new Object[] { 5, 6 });

System.out.println(retVal4);

}

}

運行結果:

-?Unable?to?find?required?classes?(javax.activation.DataHandler?and?javax.mail.internet.MimeMultipart).?Attachment?supportisdisabled.

Hello?World

hello,asp?webservice

123.0

11.0

- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.

Hello World

hello,asp webservice

123.0

11.0

注意點:

(a)我們發現如果參數是String類型的,那么可以不需要設置call的參數?call3.setEncodingStyle(null);但是如果傳入參數為float類型,那么就必須加上這一條語句。

(b)設置返回值類型有兩種方式:

一種是

call.setReturnType(XMLType.SOAP_STRING);

call.setReturnType(XMLType.SOAP_STRING);

另外一種是

call2.setReturnType(newQName("http://www.w3.org/2001/XMLSchema","string"));

call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));

這兩種方法是等價的

總結

以上是生活随笔為你收集整理的java调用net webservice_java调用.net的webservice的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。