参照网上的代码如下:
package test;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Test {
public static void main(String[] args) {
String[] orderinfo=new String[1];
orderinfo[0]="上海";
String str = "";
try {
String url = "http://webservice.xp81.com/AspxTools.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setOperationName(new QName("http://tempuri.org", "GetSystem"));
call.addParameter("strUrl",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/IsURL");
str = (String) call.invoke(new Object[]{orderinfo});
} catch (Exception e) {
System.out.println("asdf");
e.printStackTrace();
}
System.out.println(str);
}
}
总是报异常 似乎是参数没有传递过去,换了几个webservice都是这样的错误。
请大家帮看看怎么解决,谢谢,这个地址是外网可用的,大家可以测试代码!
package test;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Test {
public static void main(String[] args) {
String[] orderinfo=new String[1];
orderinfo[0]="上海";
String str = "";
try {
String url = "http://webservice.xp81.com/AspxTools.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setOperationName(new QName("http://tempuri.org", "GetSystem"));
call.addParameter("strUrl",org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);
call.setUseSOAPAction(true);
call.setSOAPActionURI("http://tempuri.org/IsURL");
str = (String) call.invoke(new Object[]{orderinfo});
} catch (Exception e) {
System.out.println("asdf");
e.printStackTrace();
}
System.out.println(str);
}
}
总是报异常 似乎是参数没有传递过去,换了几个webservice都是这样的错误。
请大家帮看看怎么解决,谢谢,这个地址是外网可用的,大家可以测试代码!
参数是否在c#中定义啦。
对方的webservice是否开启
曾经碰到过Java调用C#的webservice的问题,关键在于申明QName的方式上,写的不对就会传不出去参数,而且如果你能够观察http传输流,可以很明显看出不同的QName申明方式生成的XML是不同的,这个好象是Axis的Bug。
具体咋写,让我回去查查你可以用http injection之类的工具,先登陆C#服务器的Test页面测试下,然后看发送的真实XML与你Axis生成的XML的区别,非常明显。
等你的代码了,谢谢
call.setOperationName(new QName("http://tempuri.org", "GetSystem"));
修正为:
call.setOperationName(new QName("http://tempuri.org", "IsURL"));
问题可能出在这句上。
我以前写代码自己封装了一下。 /**
*
* @param methodName 执行的方法名
* @param paremate 参数数据数组
* @param qNameList 变量命名空间List
* @param actionUrl soapActionUrl
* @return
*/
public Object invokeMehodWithParemate(String methodName, Object[] paremate, List qNameList, String actionUrl){
String endPoint = this.getSERVICE_URL();
Object returnObj = null;
try{
Service service = new Service();
Call call = null;
call = (Call)service.createCall(); QName qName = new QName(this.NAME_SPACE,methodName);
call.setOperationName(qName);
call.setSOAPActionURI(actionUrl);
for(int i=0;i<qNameList.size();i++){
call.addParameter((QName)qNameList.get(i), org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
}
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
call.setTargetEndpointAddress(new URL(endPoint));
returnObj = call.invoke(paremate);
}catch(Exception ie){
ie.printStackTrace();
}
return returnObj;
}
红色的部分,你试试,addParemate的时候传入的是QName类型,而不是String
javax.xml.rpc.ParameterMode.IN);
String actionUlr = "http://tempuri.org/GetMainClassName";
Object[] paremate = new Object[] { userName, passWord, mainClassId };
List qNameList = new ArrayList();
qNameList.add(new QName(webServiceUtil.getNAME_SPACE(), "UserName"));
qNameList.add(new QName(webServiceUtil.getNAME_SPACE(), "UserPwd"));
qNameList.add(new QName(webServiceUtil.getNAME_SPACE(), "MainClassID"));
String className = (String) webServiceUtil.invokeMehodWithParemate(
"GetMainClassName", paremate, qNameList, actionUlr);
return className;
}
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service; public class Test {
public static void main(String[] args) {
String[] orderinfo=new String[1];
orderinfo[0]="上海";
String str = "";
try {
String url = "http://webservice.xp81.com/AspxTools.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setOperationName(new QName("http://tempuri.org/", "IsURL"));
call.addParameter(new QName("http://tempuri.org/", "strUrl"), 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("http://tempuri.org/IsURL");
str = (String) call.invoke(orderinfo); } catch (Exception e) {
System.out.println("asdf");
e.printStackTrace();
}
System.out.println(str);
}
}
to sunshinealways:呵呵,你懂我的意思了,我就是要你这样的方式,谢谢哥们。