`

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


分享到:
评论
2 楼 yuyangtina 2012-11-09  
哦,知道了,是继承的方法。谢谢你的分享。
1 楼 yuyangtina 2012-11-09  
请教个问题,您说postMethod.setRequestBody("")已经被方法setRequestEntity方法代替了。 我在官方网站下载的jar包commons-httpclient-3.0.1.jar,postMethod类中只有setRequestBody 没看到setRequestEntity这个方法,为什么?

相关推荐

Global site tag (gtag.js) - Google Analytics