`

WebService

阅读更多
最近开发WebService,对方webservice升级,我这边客户端接口不能使用,报服务器无法识别Http开头soapAction的值,先开始想着写一个webservice模拟出问题,主要是对方服务不可以访问,公司为内网,先开始写了个bat脚本进行测试,soapAction赋值,不对的话还是不能识别。soapAction赋值应该为 访问webservice比如
http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl
从里面找到方法对应的soapAction的值
这样写了后没有再报以前的错,但是又出现了新的错误,搜过好多文章,发现说的都比较笼统,后经过试验是这样的,花费了我好长时间(真是蛋疼)直接上代码
public static void main(String[] args) {
// http://WebXml.com.cn/是wsdl中definitions根节点的targetNamespace属性 
try{      
   String cityCode ="";
   String userId = "";
   // webservice路径  
    // 这里后面加不加 "?wsdl" 效果都一样的 
   String endpoint = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx";         
    String[] res = null;  
     
    // 查询城市天气的接口方法名  
    String operationName = "getWeather";  
    // 定义service对象 
    Service service = new Service();  
    // 创建一个call对象 
    Call call = (Call) service.createCall();  
    // 设置目标地址,即webservice路径  
    call.setTargetEndpointAddress(endpoint);  
    // 设置操作名称,即方法名称  
    call.setOperationName(operationName);  
    // 设置方法参数  
    call.addParameter( new QName("http://WebXml.com.cn/","theCityCode"), 
    org.apache.axis.encoding.XMLType.XSD_STRING,  
    javax.xml.rpc.ParameterMode.IN);  
    call.addParameter( new QName("http://WebXml.com.cn/","theUserID"), 
            org.apache.axis.encoding.XMLType.XSD_STRING,  
            javax.xml.rpc.ParameterMode.IN);  
    // 设置返回值类型  
    //对于返回是字符串数组的返回类型只有这两种可行 
     
   //call.setReturnType(org.apache.axis.encoding.XMLType.SOAP_VECTOR); 
    call.setReturnClass(java.lang.String[].class); 
    
    call.setUseSOAPAction(true);  
    call.setSOAPActionURI("http://WebXml.com.cn/"+"getWeather"); 
    
    res=(String[]) call.invoke(new Object[]{cityCode,userId});  
     
    // 如果返回类型是org.apache.axis.encoding.XMLType.SOAP_VECTOR时用下面的转型接收 
    //Vector v=(Vector) call.invoke(new Object[]{cityCode,userId});  
    for(String str:res) 
    { 
        System.out.println(str+"===================="); 
    } 




}catch(Exception e){
e.printStackTrace();
}
}

如果运行上面代码会发现出以下异常
服务器无法处理请求。 ---> 值不能为空。
参数名: input
把call.setOperationName(operationName);  
改为call.setOperationName(new QName("http://WebXml.com.cn/",operationName)); 
运行正常,传递参数也是如此加上new QName,据我撸测是安全验证,如果有的加有的不加就会出现上面的错误,终于解决了,希望大家少走弯路。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics