HttpClient4 Post XML数据
2012-04-20 17:02
381 查看
最近项目中用到SOAP,要求客户端POST SOAP数据过去,整理一下自己写的东西。
POST XML一般有两种方法,一种是指定参数名,将该参数来进行XML数据的传输,这是最常用的一种方式。
这次我想说明的另外一种,直接将XML数据以流的方式写入请求。
Servlet POST方法中来接受传送过来的XML流:
Client端POST XML过去:
OK,大功告成,很简单吧。
POST XML一般有两种方法,一种是指定参数名,将该参数来进行XML数据的传输,这是最常用的一种方式。
这次我想说明的另外一种,直接将XML数据以流的方式写入请求。
Servlet POST方法中来接受传送过来的XML流:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/xml"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); System.out.println("----------------------"); BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8")); String line = null; StringBuffer sb = new StringBuffer(); while ((line = reader.readLine()) != null) { sb.append(line).append("\r\n"); } System.out.println(sb.toString()); System.out.println("----------------------"); out.print(sb.toString()); out.flush(); out.close(); }
Client端POST XML过去:
package com.javaeye.wangking717.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; public class HttpConnectionUtil { private final static Log logger = LogFactory.getLog(HttpConnectionUtil.class); public static String postSOAP(String url, String soapContent) { HttpClient httpclient = null; HttpPost httpPost = null; BufferedReader reader = null; int i = 0; while (i < 4) { try { httpclient = new DefaultHttpClient(); httpPost = new HttpPost(url); StringEntity myEntity = new StringEntity(soapContent, "UTF-8"); httpPost.addHeader("Content-Type", "text/xml; charset=UTF-8"); httpPost.setEntity(myEntity); HttpResponse response = httpclient.execute(httpPost); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { reader = new BufferedReader(new InputStreamReader(resEntity .getContent(), "UTF-8")); StringBuffer sb = new StringBuffer(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); sb.append("\r\n"); } return sb.toString(); } } catch (Exception e) { i++; if (i == 4) { logger.error("not connect:" + url + "\n" + e.getMessage()); } } finally { if (httpPost != null) { httpPost.abort(); } if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } if (httpclient != null) { httpclient.getConnectionManager().shutdown(); } } } return "none"; } public static void main(String[] args) { String url = "http://localhost:8080/opgtest/servlet/MyTest"; String soap = "<xml>\r\n" + "<body>\r\n" + "传递过来的内容\r\n" + "</body>\r\n" + "</xml>"; System.out.println(postSOAP(url, soap)); } }
OK,大功告成,很简单吧。
相关文章推荐
- HttpClient4 Post XML数据
- Java 通过httpClient Post方式提交xml,并从服务端返回数据
- HttpClient通过Post方式发送Json数据
- HTTPCLIENT POST 返回信息处理 避免返回数据过大
- Android之向服务器提交数据(POST,GET,AsyncHttpClient)
- HttpClient使用post方式模拟表单提交数据到服务器并下载服务器文件
- HttpClient4 Post XML到一个服务器上
- 解析HTTP POST方式请求的xml数据、本地XML文件数据, 并返回JSON字符串
- HttpUrlConnection与HttpClient的post、get的请求网络数据的方法
- Java 通过HttpURLConnection Post方式提交xml,并从服务端返回数据
- android get或post及HttpClient与服务器数据交互
- JAVA 爬虫之httpclient post请求提交表单获取Ajax数据
- httpClient 发送 post请求 json数据
- HttpClient4 Post XML到一个服务器上
- HttpClient4 Post XML到一个服务器上
- HttpClient的get和post请求数据
- HttpClient 4 使用POST方式提交普通表单数据的例子
- HttpClient 使用 java -- POST方式带参提交数据
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)