JAVA 调用 .NET编写的WebService
1..NET编写的WebService源码
代码
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<SoapDocumentService(RoutingStyle:=SoapServiceRoutingStyle.RequestElement)> _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld(ByVal para_in1 As String, ByVal para_in2 As String) As String
Try
Dim s As String = "para_in1:" & para_in1.ToString() & ",para_in2=" & para_in2 & vbCrLf
My.Computer.FileSystem.WriteAllText("c:\ws.txt", s, True)
Return s
Catch ex As Exception
My.Computer.FileSystem.WriteAllText("c:\ws.txt", "ex:" & ex.ToString() & vbCrLf, True)
Return ex.ToString()
End Try
Return "NO"
End Function
End Class
Imports System.Web.Services.Protocols
Imports System.ComponentModel
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<SoapDocumentService(RoutingStyle:=SoapServiceRoutingStyle.RequestElement)> _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld(ByVal para_in1 As String, ByVal para_in2 As String) As String
Try
Dim s As String = "para_in1:" & para_in1.ToString() & ",para_in2=" & para_in2 & vbCrLf
My.Computer.FileSystem.WriteAllText("c:\ws.txt", s, True)
Return s
Catch ex As Exception
My.Computer.FileSystem.WriteAllText("c:\ws.txt", "ex:" & ex.ToString() & vbCrLf, True)
Return ex.ToString()
End Try
Return "NO"
End Function
End Class
2.JAVA调用源码:
代码
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class myMain2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
InvokeWebservice();
}
public static void InvokeWebservice()
{
try
{
/**说明:
http://127.0.0.1:4727/Service1.asmx 表示Dotnet的WebService地址
http://tempuri.org/ 表示命名空间
HelloWorld 表示调用的函数名
para_in1 表示 HelloWorld 函数的第一个参数名
para_in2 表示 HelloWorld 函数的第二个参数名
另:本源码需要引用三个JAR包(axis.jar, jaxrpc.jar, commons-discovery-0.2.jar);
* */
String endpoint = "http://127.0.0.1:4727/Service1.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.addParameter(new QName("http://tempuri.org/","para_in1"), org.apache.axis.Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/","para_in2"), org.apache.axis.Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
call.setOperationName(new QName("http://tempuri.org/","HelloWorld"));
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/HelloWorld");
String para_in1 = "Java_Invoke_DotnetWebservice";
String para_in2 = "2011-01-21";
String s = (String)call.invoke(new Object[]{para_in1, para_in2});
System.out.println("result is " + s);
}
catch (Exception e)
{
System.err.println(e.toString());
e.printStackTrace();
}
}
}
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class myMain2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
InvokeWebservice();
}
public static void InvokeWebservice()
{
try
{
/**说明:
http://127.0.0.1:4727/Service1.asmx 表示Dotnet的WebService地址
http://tempuri.org/ 表示命名空间
HelloWorld 表示调用的函数名
para_in1 表示 HelloWorld 函数的第一个参数名
para_in2 表示 HelloWorld 函数的第二个参数名
另:本源码需要引用三个JAR包(axis.jar, jaxrpc.jar, commons-discovery-0.2.jar);
* */
String endpoint = "http://127.0.0.1:4727/Service1.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.addParameter(new QName("http://tempuri.org/","para_in1"), org.apache.axis.Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName("http://tempuri.org/","para_in2"), org.apache.axis.Constants.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.Constants.XSD_STRING);
call.setOperationName(new QName("http://tempuri.org/","HelloWorld"));
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/HelloWorld");
String para_in1 = "Java_Invoke_DotnetWebservice";
String para_in2 = "2011-01-21";
String s = (String)call.invoke(new Object[]{para_in1, para_in2});
System.out.println("result is " + s);
}
catch (Exception e)
{
System.err.println(e.toString());
e.printStackTrace();
}
}
}
实例下载: Java_Invoke_DotnetWebservice.rar