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方法获取的返回消息已经没有响应头部了,直接就是附件的二进制流。


使用httpclient3.x和apache的fileuploade控件上传文件的方法(必须在容器里面)

发送请求:
HttpClient httpClient = HttpClientUtil.getInstance();
PostMethod postMethod = null;
File dest = new File(timestampFile, targetFile.getName())
String uri = "http://10.10.10.10:8080/console/centrolServlet"
postMethod = new PostMethod(uri);
Part[] parts = { new FilePart(projectName, dest) };
postMethod.setRequestEntity(new MultipartRequestEntity(parts,postMethod.getParams()));
int status = httpClient.executeMethod(postMethod);


接受请求:
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
File tmp = new File(defaultFile, "tmp");
File uploadFile = new File(tmp, uploadPath);
factory.setRepository(tmp);
List<FileItem> items;
items = upload.parseRequest((HttpServletRequest) request);
Iterator<FileItem> files = items.iterator();

while (files.hasNext())
{
FileItem fi = (FileItem) files.next();
String fileName = fi.getName();
if (fileName != null)
{
File fullFile = new File(fi.getName());
saveFile = new File(uploadFile, fullFile.getName());
fi.write(saveFile);
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值