利用HTTPCLIENT调用WEBSERVICE接口,结果出现报错no SOAPAction header!
百度查了下,说是axis1.4的bug,于是反编译查看了源码,发现需要获取header SOAPAction,于是加了就可以成功了
代码中的1.xml实际上是soap的报文
例如:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.autotest.asiainfo.com">
<soapenv:Header/>
<soapenv:Body>
<web:queryUserById>
<web:userId>1111</web:userId>
</web:queryUserById>
</soapenv:Body>
</soapenv:Envelope>
java代码如下:
Map<String,String> map = new HashMap<String,String>();
try {
httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.7.5.56:1808/AutoTestWeb/services/AutoTestWebservice?wsdl"); //webservice服务地址
String xml = FileUtils.readFileToString(new File("e:/1.xlml"));//
String soapRequestData = xml; //soap协议的格式,定义了方法和参数
HttpEntity re = new StringEntity(soapRequestData, HTTP.UTF_8);
httppost.setHeader("Content-Type","application/soap+xml; charset=utf-8");
httppost.setHeader("SOAPAction","application/soap+xml; charset=utf-8");//随便填,必须要有值
httppost.setEntity(re);
HttpResponse response = httpClient.execute(httppost); //调用接口
System.out.println(response.getStatusLine().getStatusCode());
if(response.getStatusLine().getStatusCode() == 200) { //调用状态
String xmlString = EntityUtils.toString(response.getEntity());
System.out.println("===="+xmlString);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown(); //关闭连接
}
}