博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java--HttpClient
阅读量:5010 次
发布时间:2019-06-12

本文共 3806 字,大约阅读时间需要 12 分钟。

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     @Override13     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  2  3  4 
5 WebTest login 6 7 8

保存

9
10
11
12
13 14

简单的提交页面。

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

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

这里简单的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
params=new ArrayList
(); 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 block60 e.printStackTrace();61 }62 }63 }64 65 }

 

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

 

转载于:https://www.cnblogs.com/microcat/p/6523973.html

你可能感兴趣的文章
Educational Codeforces Round 60 (Rated for Div. 2) C. Magic Ship
查看>>
Windows 2008 R2系统开机时如何不让Windows进行磁盘检测?
查看>>
WP7应用开发笔记(18) 本地化与多语言
查看>>
解决 .so文件64与32不兼容问题
查看>>
归并排序法
查看>>
【剑指offer】面试题26:复杂链表的复制
查看>>
spark开发生成EXE
查看>>
Vue 全家桶介绍
查看>>
WPF Bitmap转Imagesource
查看>>
Java compiler level does not match the version of the installed Java project facet.解决方法
查看>>
笔记_小结
查看>>
Linux lsof命令 umount U盘
查看>>
自定义Font
查看>>
linux svn 服务端搭建
查看>>
maven用途、核心概念、用法、常用参数和命令、扩展
查看>>
linux时间同步ntp服务的安装与配置
查看>>
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法...
查看>>
网络编程-socket并发-粘包问题
查看>>
python 中安装pandas
查看>>
Hibernate 的<generator class="native"></generator>的不同属性含义
查看>>