java调用.net的 WebService
1在vs2005中新建WebService,
在App_Code
建立新类:Class1.cs
-----using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
}
//
public String join(String s1, String s2)
{
String s = "*****" + s1 + s2 + "&&&&&&";
return s;
}
}修改:Service.csusing System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
public class Service : System.Web.Services.WebService
{
public Service () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
public string HelloWorld(String s) {
Class1 c1 = new Class1();
String sp = c1.join("我是中国",s);
return (sp+"Hello World,This WebService powered by luinstein");
}
}发布WebService
2
在Myeclipse里新建一个java项目
要使用到axis1.4
使用测试类testwebservice.java:package test;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class testwebservice {
/**
* @param args
* @throws RemoteException
* @throws ServiceException
* @throws MalformedURLException
*/
public static void main(String[] args) throws RemoteException, ServiceException, MalformedURLException {
String url="http://localhost:2019/WebServiveTest/Service.asmx";
String namespace = "http://tempuri.org/";
String methodName = "HelloWorld";
String soapActionURI = "http://tempuri.org/HelloWorld";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setUseSOAPAction(true);
//这个地方没设对就会出现Server was unable to read request的错误
call.setSOAPActionURI(soapActionURI);
call.setOperationName(new QName(namespace, methodName));
/*这里如果设置成call.addParameter(new QName(namespace,"s"), XMLType.XSD_STRING,
ParameterMode.IN);就是调用document风格的.net服务端
如果设反了,.net服务端就接不到参数,接到的是null
*/
//call.addParameter("s", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName(namespace,"s"), XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
String ret = (String) call.invoke(new Object[] { "kusix" });
System.out.println("返回结果---> " + ret);
}
}
页:
[1]