Java--HttpClient

 1 package main;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 public class Save extends HttpServlet {
11 
12     @Override
13     protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
14         req.getRequestDispatcher("WEB-INF/save.html").forward(req, res);
15     }
16 
17     
18     
19 }
package main;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ToSave extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        System.out.println(req.getParameter("t"));
        req.getRequestDispatcher("WEB-INF/save.html").forward(req, res);
    }

    
    
}
 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>WebTest login</title>
 6 </head>
 7 <body>
 8     <h2>保存</h2>
 9     <form action="toSave.do" method="post">
10         <textarea name="t" cols="188" rows="5"></textarea>
11         <input type="submit" value="保存" />
12     </form>
13 </body>
14 </html>

简单的提交页面。

通过分析,可以知道保存时候发生的请求。

这里也可以知道传递了那些参数。

这里简单的demo只需要穿文本区的内容就可以了。

 1 package httpclient.demo;
 2 
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import org.apache.http.Header;
 8 import org.apache.http.HttpEntity;
 9 import org.apache.http.NameValuePair;
10 import org.apache.http.client.ClientProtocolException;
11 import org.apache.http.client.entity.UrlEncodedFormEntity;
12 import org.apache.http.client.methods.CloseableHttpResponse;
13 import org.apache.http.client.methods.HttpGet;
14 import org.apache.http.client.methods.HttpPost;
15 import org.apache.http.impl.client.CloseableHttpClient;
16 import org.apache.http.impl.client.HttpClients;
17 import org.apache.http.message.BasicNameValuePair;
18 import org.apache.http.protocol.HTTP;
19 import org.apache.http.util.EntityUtils;
20 
21 public class LoginDemo4 {
22 
23     public static void main(String[] args) {
24         // HttpClientBuilder.create().build()
25         CloseableHttpClient httpClient = null;
26         HttpPost post = null;
27         try {
28             httpClient = HttpClients.createDefault();
29             //登录
30             post = new HttpPost("http://localhost:8080/WEB_TEST/toSave.do");
31             post.setHeader("User-Agent",
32                     "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
33             
34             //传递所需参数
35             List<NameValuePair> params=new ArrayList<NameValuePair>();  
36             params.add(new BasicNameValuePair("t","Hello World!!"));  
37             
38             post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
39             CloseableHttpResponse response = httpClient.execute(post);
40             HttpEntity entity = response.getEntity();
41             if (entity != null) {
42                 String str = EntityUtils.toString(entity, "utf-8");
43                 System.out.println(str);
44             }
45             
46         } catch (ClientProtocolException e) {
47             e.printStackTrace();
48         } catch (IOException e) {
49             e.printStackTrace();
50         } finally {
51             try {
52                 if (post != null) {
53                     post.releaseConnection();
54                 }
55                 if (httpClient != null) {
56                     httpClient.close();
57                 }
58             } catch (IOException e) {
59                 // TODO Auto-generated catch block
60                 e.printStackTrace();
61             }
62         }
63     }
64 
65 }

 运行结果可知,httpclient的demo成功发出了保存请求,并获取到了传递的参数,即文本区域内容。

原文地址:https://www.cnblogs.com/microcat/p/6523973.html