httpclent调用 webservice wsdl后缀服务
1.jar包:
commons-logging-1.2.jar
commons.codec_1.3.jar
commons.httpclient_3.1.jar
dom4j-1.4.jar
httpcore_4.0-beta1.jar
2.地址:格式如【http://192.168.123.89:7031/cus/EmvsWs?wsdl】
3.soap类型的xml字符串
用soapui生成,右侧的就是
红框内的部分就是soap类型的xml字符串,拷贝到代码中,当然要将你的参数替换到 ?用拼接
4. 请求和传输时间自己随意设置。
5. soapAction就不用管,直接“”
6. 返回值就是一串字符串,类似上图中的红框下边那一部分,将它转换成document。再取得其中的值就可以了
/**
postUrl:远程地址
soapXml:soap类型的xml字符串
soapAction:默认””
socketTimeout:请求超时时间
connectTimeout:传输超时时间
*/
public static String doPostSoap1_1(String postUrl, String soapXml,
String soapAction, String socketTimeout, String connectTimeout) {
String retStr = "";
//HttpClient
HttpClient httpClient =new HttpClient();
//PostMethod
PostMethod postMethod =new PostMethod(postUrl);
// 设置请求和传输超时时间
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(Integer.parseInt(socketTimeout));//连接超时
httpClient.getHttpConnectionManager().getParams().setSoTimeout(Integer.parseInt(connectTimeout));
//设置请求体
try {
RequestEntity requestEntity = new ByteArrayRequestEntity(soapXml.getBytes("utf-8"));
postMethod.setRequestEntity(requestEntity);
// postMethod.setRequestBody(soapXml); //方法过时
//设置请求参数
postMethod.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
postMethod.setRequestHeader("SOAPAction", soapAction);
httpClient.executeMethod(postMethod); //发送请求
retStr=postMethod.getResponseBodyAsString();//响应体
System.out.println("retStr:"+retStr);
//将相应体转换成document解析,并取得其中的数据
retStr =DocumentHelper.parseText(retStr).getRootElement().element("Body").element("uploadCusxDownUpInfoResponse").element("result").getText();
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭链接
if(postMethod != null)
postMethod.releaseConnection();
}
System.out.println("retStr:"+retStr);
return retStr;
}