HttpClient4 Post XML数据

本文介绍了一种通过HttpClient4发送SOAP请求的方法。具体实现包括客户端构造SOAP请求并将其作为XML数据发送,以及服务器端接收并解析这些XML数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HttpClient4 Post XML数据

最近项目中用到SOAP,要求客户端POST SOAP数据过去,整理一下自己写的东西。

 

POST XML一般有两种方法,一种是指定参数名,将该参数来进行XML数据的传输,这是最常用的一种方式。

 

这次我想说明的另外一种,直接将XML数据以流的方式写入请求。

 

Servlet POST方法中来接受传送过来的XML流:

 

Java代码   收藏代码
  1. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  2.   
  3.         response.setContentType("text/xml");    
  4.         response.setCharacterEncoding("UTF-8");    
  5.         PrintWriter out = response.getWriter();  
  6.         System.out.println("----------------------");    
  7.         BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));  
  8.         String line = null;  
  9.         StringBuffer sb = new StringBuffer();  
  10.         while ((line = reader.readLine()) != null) {  
  11.             sb.append(line).append("\r\n");  
  12.         }  
  13.         System.out.println(sb.toString());    
  14.         System.out.println("----------------------");    
  15.         out.print(sb.toString());    
  16.         out.flush();  
  17.         out.close();  
  18.     }  

 

 

Client端POST XML过去:

 

Java代码   收藏代码
  1. package com.javaeye.wangking717.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6.   
  7. import org.apache.commons.logging.Log;  
  8. import org.apache.commons.logging.LogFactory;  
  9. import org.apache.http.HttpEntity;  
  10. import org.apache.http.HttpResponse;  
  11. import org.apache.http.client.HttpClient;  
  12. import org.apache.http.client.methods.HttpPost;  
  13. import org.apache.http.entity.StringEntity;  
  14. import org.apache.http.impl.client.DefaultHttpClient;  
  15.   
  16. public class HttpConnectionUtil {  
  17.   
  18.     private final static Log logger = LogFactory.getLog(HttpConnectionUtil.class);  
  19.   
  20.     public static String postSOAP(String url, String soapContent) {  
  21.   
  22.         HttpClient httpclient = null;  
  23.         HttpPost httpPost = null;  
  24.         BufferedReader reader = null;  
  25.         int i = 0;  
  26.   
  27.         while (i < 4) {  
  28.             try {  
  29.                 httpclient = new DefaultHttpClient();  
  30.                 httpPost = new HttpPost(url);  
  31.                 StringEntity myEntity = new StringEntity(soapContent, "UTF-8");  
  32.                 httpPost.addHeader("Content-Type""text/xml; charset=UTF-8");  
  33.                 httpPost.setEntity(myEntity);  
  34.                 HttpResponse response = httpclient.execute(httpPost);  
  35.                 HttpEntity resEntity = response.getEntity();  
  36.                 if (resEntity != null) {  
  37.                     reader = new BufferedReader(new InputStreamReader(resEntity  
  38.                             .getContent(), "UTF-8"));  
  39.                     StringBuffer sb = new StringBuffer();  
  40.                     String line = null;  
  41.                     while ((line = reader.readLine()) != null) {  
  42.                         sb.append(line);  
  43.                         sb.append("\r\n");  
  44.                     }  
  45.                     return sb.toString();  
  46.                 }  
  47.   
  48.             } catch (Exception e) {  
  49.                 i++;  
  50.                 if (i == 4) {  
  51.                     logger.error("not connect:" + url + "\n" + e.getMessage());  
  52.                 }  
  53.             } finally {  
  54.                 if (httpPost != null) {  
  55.                     httpPost.abort();  
  56.                 }  
  57.                 if (reader != null) {  
  58.                     try {  
  59.                         reader.close();  
  60.                     } catch (IOException e) {  
  61.                         e.printStackTrace();  
  62.                     }  
  63.                 }  
  64.                 if (httpclient != null) {  
  65.                     httpclient.getConnectionManager().shutdown();  
  66.                 }  
  67.             }  
  68.         }  
  69.         return "none";  
  70.     }  
  71.       
  72.     public static void main(String[] args) {  
  73.         String url = "http://localhost:8080/opgtest/servlet/MyTest";  
  74.         String soap = "<xml>\r\n"  
  75.                         + "<body>\r\n"  
  76.                             + "传递过来的内容\r\n"   
  77.                         + "</body>\r\n"  
  78.                     + "</xml>";  
  79.         System.out.println(postSOAP(url, soap));  
  80.   
  81.     }  
  82.   
  83. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值