HttpClient4 Post XML到一个服务器上

187 篇文章 0 订阅
68 篇文章 0 订阅

  现在网上介绍的HttpClient基本上全是3.x版本的内容,HttpClient4的API变化相对3已经变化很大,对HttpClient4做了简单的研究后,完成了一个HttpClient4 Post XML功能。

  对于POST方式,最先想到的就是表单提交了,POST XML自然想到的就是定义一个变量名,比如叫xmldata,然后将这个参数的值POST出去,在服务端接收的时候,自然也是通过 requset.getParameter("xmldata")方式来接收。

  现在我在这里要做的不是通过上面的方式,而是不指定参数名来Post,实际上就是将一个流写入请求。

  下面是具体的实现方式:

  1、参数名方式POST XML数据

 

import org.<a href="http://tech.ddvip.com/server/apache/index.html" target="_blank">apache</a>.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(); 
                } 
        } 
}


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


服务端接收方式:

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


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>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient一个用于发送HTTP请求的类库,它提供了丰富的功能和灵活的配置选项。在使用HttpClient发送POST请求时,可以按照以下步骤进行操作: 1. 创建HttpClient对象:首先需要创建一个HttpClient对象,可以通过HttpClientBuilder来构建。 2. 创建HttpPost对象:接下来创建一个HttpPost对象,并设置请求的URL。 3. 设置请求参数:如果需要向服务器发送参数,可以通过NameValuePair或者HttpEntity来设置请求参数。NameValuePair是一个键值对,可以用于设置表单参数;HttpEntity可以用于设置请求体参数,例如JSON或者XML格式的数据。 4. 设置请求头:如果需要设置请求头,可以通过HttpPost的setHeader方法来设置。 5. 执行请求:使用HttpClient的execute方法执行请求,并将返回结果保存在HttpResponse对象中。 6. 处理响应:从HttpResponse对象中获取响应状态码、响应头和响应体等信息,并进行相应的处理。 下面是一个示例代码,演示了如何使用HttpClient发送POST请求: ```java import org.apache.http.HttpEntity; 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.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.util.ArrayList; import java.util.List; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost httpPost = new HttpPost("http://example.com/api/post"); try { // 设置请求参数 List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair("username", "admin")); params.add(new BasicNameValuePair("password", "123456")); httpPost.setEntity(new UrlEncodedFormEntity(params)); // 执行请求 HttpResponse response = httpClient.execute(httpPost); // 处理响应 int statusCode = response.getStatusLine().getStatusCode(); String responseBody = EntityUtils.toString(response.getEntity()); System.out.println("Status Code: " + statusCode); System.out.println("Response Body: " + responseBody); } catch (Exception e) { e.printStackTrace(); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值