1.开发C#.net的WebService服务
1.1点击“开始”->“程序”-> "Microsoft Visual Studio 2005" -> "Microsoft Visual Studio 2005",打开.net界面
1.2 选择“文件”-> “新建” -> “网站”,选择“ASP.NET Web服务”,命名为WebServiceHelloWord,“确定”;
1.3 WebService工程建好后,能看到默认的HelloWord方法
[WebMethod]
public string HelloWorld(string) {
return "Hello World ";
}
修改此方法为:
[WebMethod]
[SoapDocumentMethodAttribute(Action = "", RequestNamespace = "", ResponseNamespace = "", ResponseElementName = "arithmeticMeanResponse", Use = SoapBindingUse.Literal)]
public string HelloWorld(string name) {
return "Hello World " + name;
}
增加参数name和SoapDocumentMethodAttribute设置,注意SoapDocumentMethodAttribute必须设置,否则java调用C#.net时会调用不成功。
1.4 编译并部署到服务器的IIS上,命名的名字为websh,地址为
2.开发java客户端
2.1 新建java工程WbeServiceTest,新建java类WST,具体代码如下
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class WST{
/**
* @param args
* @throws ServiceException
* @throws MalformedURLException
* @throws RemoteException
*/
public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {
// TODO Auto-generated method stub
// WebService URL
String service_url = "";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(service_url));
// 设置要调用的方法
call.setOperationName(new QName("", "HelloWorld"));
// 该方法需要的参数
call.addParameter(new QName(""), org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
// 方法的返回值类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setUseSOAPAction(true);
call.setSOAPActionURI("");
// 调用该方法
String res = call.invoke(new Object[] {"rock"}).toString();
System.out.println(" Result: " + res.toString());
}
}
2.2 客户端开发完毕,执行这个类,就能看到返回的结果: Result: Hello World rock 表明调用成功
在java调用C#.net开发的WebService过程中,特别要配置[SoapDocumentMethodAttribute(...)]