Java调用doNet webService方法
doNet的webService
浏览器访问测试地址:http://192.168.4.17/JLWWS/sendCommand.asmx,出现
点击getDeviceValue方法,出现
上图的xml代码再贴一遍:
POST /JLWWS/sendCommand.asmx HTTP/1.1 Host: 192.168.4.17 Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/getDeviceValue" <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getDeviceValue xmlns="http://tempuri.org/"> <n>int</n> <s>string</s> </getDeviceValue> </soap:Body> </soap:Envelope> HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getDeviceValueResponse xmlns="http://tempuri.org/"> <getDeviceValueResult>string</getDeviceValueResult> </getDeviceValueResponse> </soap:Body> </soap:Envelope>
然后
java方法:
public String cc(){ String result = ""; try{ Service service = new Service(); Call call = (Call) service.createCall(); call.setOperationName(new QName("http://tempuri.org/", "getDeviceValue")); call.addParameter(new QName("http://tempuri.org/", "n"), org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN); call.addParameter(new QName("http://tempuri.org/", "s"), org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN); call.setTargetEndpointAddress(new URL("http://192.168.4.17/JLWWS/sendCommand.asmx")); call.setUseSOAPAction(true); call.setSOAPActionURI("http://tempuri.org/getDeviceValue"); //SOAPService soap = new SOAPService(); //soap.setName("sendCommand1"); //call.setSOAPService(soap); result = (String) call.invoke(new Object[] { "1", "aaaaaaaaa"}); System.out.println(result); }catch(Exception e){ e.printStackTrace(); } return result; }
注意上述代码的红色部分。第一、命名空间注意不要写错了;第二、参数如果是int类型的话,设置值的时候用String的方式入参,(addParameter的时候还是要org.apache.axis.encoding.XMLType.XSD_INT)。
参考csdn论坛上有个帖子是这么说的:
错误信息如下:
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Client
faultSubcode:
faultString: 服务器无法读取请求。 ---> XML 文档(1, 582)中有错误。 ---> 输入字符串的格式不正确。
==================
虽然xml中是
<soap:Body>
<PubClientLogin xmlns="http://NewCap.com/NewCapecWebService/">
<appid>int</appid>
</PubClientLogin>
</soap:Body>
但是传入参数要输入string类型,
call.addParameter(new QName(targetNameSpace, "appid"), XMLType.XSD_INT, ParameterMode.IN);
call.invoke(new Object[]{"43"})
帖子地址:http://bbs.csdn.net/topics/360243982