1.C#的服務源代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace TestWebService01
{
/// <summary>
/// Service1 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
[System.Web.Script.Services.ScriptService]
public class HelloSerivce : System.Web.Services.WebService
{
private int age;
private string name;
public int Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
[WebMethod]
public string helloWorld()
{
return "Hello World";
}
[WebMethod]
public string showMyInfo(string name,int age)
{
this.Age = age;
this.Name = name;
return "My name is " + this.Name + ",my age is " + this.Age;
}
}
}
2.java的調用服務代碼:
package javaRevokeNetService;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class testService {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//注意:這裡url應該填寫vs中綁定的ip和動态端口
String wsUrl = "http://localhost:30886/HelloSerivce.asmx";
String nameSpace = "http://tempuri.org/";
String opName = "showMyInfo";
String SOAPActionURI = nameSpace + opName;
//初始化調用服務的元件
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(wsUrl));
call.setSOAPActionURI(SOAPActionURI);
call.setOperationName(new QName(nameSpace, opName));
String param1 = "name";
String paramVal1 = "小天才";
String param2 = "age";
String paramVal2 = "23";
String result = "";
call.addParameter(new QName(nameSpace, param1), XMLType.XSD_STRING,ParameterMode.IN);
call.addParameter(new QName(nameSpace, param2), XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnClass(String.class);
//調用WebService
result = (String) call.invoke(new Object[] { paramVal1, paramVal2 });
System.out.print(result);
} catch (Exception e) {
// TODO: handle exception
System.out.print(e);
}
}
}
3.聯調的結果:
一個由C#釋出的asmx服務和JAVA調用該服務的demo。簡單易懂,可以直接運作進行聯合調試。
一個C#釋出服務和JAVA調用服務的demo.rar
(缺分,謝謝給一分。如有需要,可m本人直接發)
下載下傳連結:點選打開連結
謝謝!