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,这样服务端才能识别,否则是乱码
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值