HttpClient4 Post XML到一个服务器上
现在网上介绍的HttpClient基本上全是3.x版本的内容,HttpClient4的API变化相对3已经变化很大,对HttpClient4做了简单的研究后,完成了一个HttpClient4 Post XML功能。
对于POST方式,最先想到的就是表单提交了,POST XML自然想到的就是定义一个变量名,比如叫xmldata,然后将这个参数的值POST出去,在服务端接收的时候,自然也是通过 requset.getParameter("xmldata")方式来接收。
现在我在这里要做的不是通过上面的方式,而是不指定参数名来Post,实际上就是将一个流写入请求。
下面是具体的实现方式:
1、参数名方式POST XML数据
import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.*;
/**
* 通过指定参数名的方式POST XML
*
* @author leizhimin 2010-7-8 22:29:28
*/
public class TestPost {
public static void main(String[] args) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080/waitsrv/GenXmlServlet");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("xmldate", "<html>你好啊啊</html>"));
formparams.add(new BasicNameValuePair("info", "xxxxxxxxx"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "GBK");
// entity.setContentType("text/xml; charset=GBK");
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
System.out.println(new String(buff, 0, length));
httpclient.getConnectionManager().shutdown();
}
}
}
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.*;
/**
* 通过指定参数名的方式POST XML
*
* @author leizhimin 2010-7-8 22:29:28
*/
public class TestPost {
public static void main(String[] args) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080/waitsrv/GenXmlServlet");
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("xmldate", "<html>你好啊啊</html>"));
formparams.add(new BasicNameValuePair("info", "xxxxxxxxx"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "GBK");
// entity.setContentType("text/xml; charset=GBK");
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
System.out.println(new String(buff, 0, length));
httpclient.getConnectionManager().shutdown();
}
}
}
2、不指定参数名的方式来POST数据
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 不指定参数名的方式来POST数据
*
* @author leizhimin 2010-7-8 3:22:53
*/
public class TestPostXml {
public static void main(String[] args) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080/waitsrv/GenXmlServlet");
StringEntity myEntity = new StringEntity("<html>你好啊啊</html>", "GBK");
httppost.addHeader("Content-Type", "text/xml");
httppost.setEntity(myEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
System.out.println(new String(buff, 0, length));
}
httpclient.getConnectionManager().shutdown();
}
}
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 不指定参数名的方式来POST数据
*
* @author leizhimin 2010-7-8 3:22:53
*/
public class TestPostXml {
public static void main(String[] args) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080/waitsrv/GenXmlServlet");
StringEntity myEntity = new StringEntity("<html>你好啊啊</html>", "GBK");
httppost.addHeader("Content-Type", "text/xml");
httppost.setEntity(myEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
System.out.println(new String(buff, 0, length));
}
httpclient.getConnectionManager().shutdown();
}
}
服务端接收方式:
package com;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* 接收XLM请求
*
* @author leizhimin 2010-7-8 1:02:42
*/
public class GenXmlServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// String xml = req.getParameter("xmldata");
resp.setContentType("text/xml");
resp.setCharacterEncoding("GBK");
PrintWriter out = resp.getWriter();
// out.println(xml);
// System.out.println(xml);
System.out.println("----------------------");
InputStreamReader reader = new InputStreamReader(req.getInputStream(), "GBK");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
String x = new String(buff, 0, length);
System.out.println(x);
out.print(x);
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!!</h1>");
out.println("</body>");
out.println("</html>");
}
}
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* 接收XLM请求
*
* @author leizhimin 2010-7-8 1:02:42
*/
public class GenXmlServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// String xml = req.getParameter("xmldata");
resp.setContentType("text/xml");
resp.setCharacterEncoding("GBK");
PrintWriter out = resp.getWriter();
// out.println(xml);
// System.out.println(xml);
System.out.println("----------------------");
InputStreamReader reader = new InputStreamReader(req.getInputStream(), "GBK");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
String x = new String(buff, 0, length);
System.out.println(x);
out.print(x);
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!!</h1>");
out.println("</body>");
out.println("</html>");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>GenXmlServlet</servlet-name>
<servlet-class>com.GenXmlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GenXmlServlet</servlet-name>
<url-pattern>/GenXmlServlet</url-pattern>
</servlet-mapping>
</web-app>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>GenXmlServlet</servlet-name>
<servlet-class>com.GenXmlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GenXmlServlet</servlet-name>
<url-pattern>/GenXmlServlet</url-pattern>
</servlet-mapping>
</web-app>
3、在2的基础,上改为单线程重用连接模式
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 不指定参数名的方式来POST数据,单线程重用连接模式
*
* @author leizhimin 2010-7-8 3:22:53
*/
public class TestPostXml2 {
public static void main(String[] args) throws IOException {
SingleClientConnManager sccm =new SingleClientConnManager();
HttpClient httpclient = new DefaultHttpClient(sccm);
// HttpGet httpget = new HttpGet(urisToGet[i]);
// HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080/waitsrv/GenXmlServlet");
StringEntity myEntity = new StringEntity("<html>你好啊啊</html>", "GBK");
httppost.addHeader("Content-Type", "text/xml");
httppost.setEntity(myEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
System.out.println(new String(buff, 0, length));
}
httpclient.getConnectionManager().shutdown();
}
}
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 不指定参数名的方式来POST数据,单线程重用连接模式
*
* @author leizhimin 2010-7-8 3:22:53
*/
public class TestPostXml2 {
public static void main(String[] args) throws IOException {
SingleClientConnManager sccm =new SingleClientConnManager();
HttpClient httpclient = new DefaultHttpClient(sccm);
// HttpGet httpget = new HttpGet(urisToGet[i]);
// HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080/waitsrv/GenXmlServlet");
StringEntity myEntity = new StringEntity("<html>你好啊啊</html>", "GBK");
httppost.addHeader("Content-Type", "text/xml");
httppost.setEntity(myEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1");
char[] buff = new char[1024];
int length = 0;
while ((length = reader.read(buff)) != -1) {
System.out.println(new String(buff, 0, length));
}
httpclient.getConnectionManager().shutdown();
}
}
相关推荐
本示例探讨的是如何使用HTTPClient库在客户端发送XML报文到服务器端,且报文通过POST方法传递,并可能涉及加密处理,确保数据的安全性。 首先,HTTPClient是Apache提供的一款强大的Java HTTP客户端API,它允许...
至此,我们已经成功地使用Java的HttpClient库将一个对象转换为XML字符串,并以二进制流的方式发送到了服务器。这个过程中涉及的关键技术包括对象到XML的转换(JAXB)、流操作(ByteArrayOutputStream和...
在使用HttpClient发送POST请求时,我们需要关心两个方面的问题:一是如何忽略证书验证过程,二是如何发送POST请求。对于前者,我们可以使用X509TrustManager来忽略证书验证过程。对于后者,我们可以使用HttpPost对象...
4. **添加依赖**:将Apache HttpClient库添加到项目的依赖中。对于使用Maven的项目,可以在`pom.xml`文件中添加如下依赖: ```xml <groupId>org.apache.httpcomponents <artifactId>httpclient <version>...
在XML、form(表单数据)和JSON提交方面,HttpClient提供了一种灵活的方式将这些数据类型发送到服务器。 首先,我们来看`HttpClientUtil.java`,这是一个常见的工具类,通常包含一系列静态方法,用于简化HttpClient...
在这个场景中,我们关注的是如何使用HttpClient来发送POST请求并传输JSON数据。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,广泛用于API接口的数据传递。 首先,我们需要引入Apache HttpClient...
接着,我们创建一个XML字符串并将其作为请求实体设置到POST方法中。最后,执行请求并处理响应。 在实际开发中,为了确保数据的安全性,可能还需要添加身份验证、设置超时、处理异常等。此外,现代Java应用通常会...
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
Java HttpClient 4,也称为Apache HttpClient 4,是Apache软件基金会开发的一个HTTP客户端实现库,用于处理与HTTP服务器的交互。这个库在Java编程环境中广泛使用,尤其对于网络请求的发送和响应的处理,提供了强大而...
以下是一个示例,展示了如何构建XML内容并作为POST请求的一部分: ```java String xmlData = "<request><param1>value1</param1><param2>value2</param2></request>"; HttpPost httpPost = new HttpPost(...
在描述中提到的“httpclient4”,指的是HttpClient 4.x系列,这是一个稳定且广泛使用的版本。 在HttpClient 4.4.1.jar中,包含了HttpClient的主要功能实现,如HTTP方法(GET, POST等)、连接管理、重定向处理、身份...
在Android开发中,HTTP通信是应用与服务器交互的基础,`HttpClient`和`HttpPost`就是其中常用的两个类,它们属于Apache的HTTP组件,虽然在Android API level 23之后被标记为过时,但由于其功能强大和灵活性高,仍被...
2. HttpPost:HttpPost是HttpClient库中的一个类,用于执行HTTP POST请求。POST请求常用于向服务器发送数据,例如提交表单或上传文件。HttpPost对象允许我们设置请求头、URL以及请求体内容。 3. HttpGet:HttpGet是...
HttpClient 4 是一个由 Apache 软件基金会开发的Java库,用于执行HTTP请求并处理响应。这个库在Web服务和API交互、自动化测试、数据抓取等场景中广泛应用。最新帮助文档通常会涵盖HttpClient 4的主要功能、用法、...
执行POST请求是通过调用`httpClient`对象的`execute`方法实现的,它接收`HttpPost`对象作为参数,返回一个`CloseableHttpResponse`,代表服务器的响应。 ```java CloseableHttpResponse response = httpClient....
在给定的代码片段中,主要展示了如何使用Java中的Apache HttpClient库发送一个包含XML数据的POST请求,并接收响应。下面是对关键部分的详细分析: 1. **导入必要的库**:代码首先导入了处理网络请求、输入输出流...
HttpClient是Java中用于执行HTTP请求的一个强大库,它提供了丰富的功能,可以方便地进行GET、POST请求,并且能够处理复杂的网络交互,包括发送文件等操作。下面我们将详细讨论HttpClientUtil工具类如何实现这些功能...
在Java编程中,Apache HttpClient库是一个非常常用的工具,用于执行HTTP请求,如GET和POST,以获取或提交网络上的数据。HttpClient提供了丰富的功能,包括设置请求头、处理重定向、管理连接池等,使得开发者可以高效...
总结起来,这些jar包共同构成了一个完整的Java HttpClient环境,能够处理各种复杂的HTTP通信任务,从简单的GET和POST请求到复杂的文件上传、多部分表单数据处理,以及与Web服务器的高级交互。开发者可以根据项目需求...