今天为了java webservice 为了调用NET,真是累死了,搞了整整一天.最后才搞定.唉,在网上搜了半天的资料,没有一个提供了正确的答案,都是抄来抄去的,或者高手们都太低调了,经验没有共享出来.!一想到花了一天的时间
搞这,心里真的不爽哦.不过开心的是,还是搞了出来.为了让以后学JAVA的朋友少找弯路,少花时间,我把心得写出来
JAVA 代码:
public static void main(String[] args) throws Exception {
String a = "a";
String endpoint = "http://192.168.0.101/ttt/WebService1.asmx ";
// 创建一个服务(service)调用(call)
Service service = new Service();
Call call = (Call) service.createCall();// 通过service创建call对象
// 设置service所在URL
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName("http://tempuri.org/ ","Add"));
//Add 是net 那边的方法 "http://tempuri.org/ " 这个也要注意Namespace 的地址,不带也会报错
call.addParameter(new QName("http://tempuri.org/ ","test"),
org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
// 这就是我搞了一天的原因所在,"test" 这个就是传参数的变量,也就是NET方面的参数,一定不要带错了
// 我当初不知道 ,以为这个参数是自己随便写的,结果总是报NULL,真是气死人了
call.setUseSOAPAction(true);
call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_STRING); //返回参数的类型
call.setSOAPActionURI("http://tempuri.org/ Add"); //这个也要注意 就是要加上要调用的方法Add,不然也会报错
// Object 数组封装了参数,参数为"This is Test!",调用processService(String arg)
String ret = (String) call.invoke(new Object[] {a});
System.out.println("--------"+ret);
}
以下是NET代码,完全为大家服务
[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 WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string Add(string test)
{
test = test;
return test;
}
[WebMethod]
public string test()
{
string temp = "10";
return temp;
}
}
解决:java webservice调用net 参数返回NULL
最新推荐文章于 2021-04-16 17:21:18 发布