HttpClient4.x发送soap请求的方法

public final static void main(String[] args) throws Exception {

     HttpClient httpclient = new DefaultHttpClient();
     HttpHost proxy = new HttpHost("proxy.test.com.cn", 80, "http");

     try {
        httpclient.getParams().
        setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
        HttpPost post = new HttpPost(
       "http://webservice.webxml.com.cn/
                        WebServices/ChinaZipSearchWebService.asmx");

        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>");
StringEntity entiy = new StringEntity(sb.toString(),"UTF-8");

post.setEntity(entiy);
post.setHeader("Content-Type","text/xml; charset=UTF-8");
post.setHeader("SOAPAction", "http://WebXml.com.cn/getSupportCity");

// System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(post);
// HttpEntity entity = response.getEntity();
            HttpEntity e=response.getEntity();
           
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
InputStream in = e.getContent();
byte sss[] = new byte[(int)e.getContentLength()];
in.read(sss);
System.out.println(new String(sss,"UTF-8"));
// FileOutputStream fo = new FileOutputStream(new File(
// "c:/sshhshshs.txt"));
// fo.write(sss);
// System.out.println(new String(sss, "GB2312"));
System.out.println("----------------------------------------");
// httpget.abort();
} finally {

httpclient.getConnectionManager().shutdown();
}
}


在发送之前必须把StringEntiry的编码方式设置为UTF-8,这样服务端才能识别,否则是乱码

你可能感兴趣的:(httpclient)