首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

HttpClient3.x发送Soap请求的步骤

2012-08-21 
HttpClient3.x发送Soap请求的方法public class TestClient {public static void main(String args[]){Http

HttpClient3.x发送Soap请求的方法
public class TestClient {

public static void main(String args[]){
HttpClient httpClient = new HttpClient();

String uri="http://webservice.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx";
PostMethod postMethod = new PostMethod(uri);

HostConfiguration hostconfig = httpClient.getHostConfiguration();
        hostconfig.setProxy("proxy.test.com.cn", 80);
        httpClient.setHostConfiguration(hostconfig);
       
       
        StringBuilder sb=new StringBuilder();
        sb.append("<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://WebXml.com.cn/">");
        sb.append("<soapenv:Header/>");
        sb.append("<soapenv:Body>");
        sb.append("<web:getSupportCity>");
        sb.append("<!--Optional:-->");
        sb.append("<web:byProvinceName>山西</web:byProvinceName>");
        sb.append("</web:getSupportCity>");
        sb.append("</soapenv:Body>");
        sb.append("</soapenv:Envelope>");
       
        postMethod.setRequestHeader("SOAPAction", "http://WebXml.com.cn/getSupportCity");
postMethod.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
       
        StringRequestEntity requestEntity=new StringRequestEntity(sb.toString());
        postMethod.setRequestEntity(requestEntity);

        int returnCode=0;
        try {
    returnCode = httpClient.executeMethod(postMethod);
    System.out.println(postMethod.getResponseBodyAsString());
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("返回状态码:"+returnCode);
}

}



postMethod.setRequestBody("")已经被方法setRequestEntity方法代替了。
头部信息只有Content-Type需要设置,其他不需要设置
SOAPAction头,如果设置必须设置正确的值,不能设置为空;要么不设置。



httpClient获取返回消息是附件的方法:

StringBuilder sb2=new StringBuilder();
sb2.append("<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/">");

        sb2.append("<soap:Body>");
        sb2.append("<enValidateImage xmlns="http://WebXml.com.cn/">");
        sb2.append("<byString>wanglei</byString>");
        sb2.append("</enValidateImage>");
        sb2.append("</soap:Body>");
        sb2.append("</soap:Envelope>");
       
       
        postMethod.setRequestHeader("SOAPAction",

"http://WebXml.com.cn/enValidateImage");
postMethod.setRequestHeader("Content-Type", "text/xml; charset=UTF

-8");
       
        StringRequestEntity requestEntity=new StringRequestEntity(sb2.toString());
        postMethod.setRequestEntity(requestEntity);

        int returnCode=0;
        try {
         returnCode = httpClient.executeMethod(postMethod);
InputStream in=postMethod.getResponseBodyAsStream();
         byte[] ims=new byte[(int)postMethod.getResponseContentLength()];
         in.read(ims);
         OutputStream out=new FileOutputStream(new File("c:\\longcxm3.gif"));
    out.write(ims);
    in.close();
    out.close();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


postMethod方法获取的返回消息已经没有响应头部了,直接就是附件的二进制流。

热点排行