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;
}