1.HttpClient可以读取网页(HTTP/HTTPS)内容

2.对url发送get/post请求(带不带参数都可以),进行测试

一、maven项目pom.xml需要引入包

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>

二、新建类进行测试

 1 public class HttpClientTest {
2
3 public static void main(String[] args) throws IOException{
4 //创建默认http请求
5 HttpClient httpClient = HttpClients.createDefault();
6 //创建post请求
7 HttpPost httpPost = new HttpPost("http://127.0.1.1:8080/goods/UserServlet");
8 //传递参数
9 List<NameValuePair> pairsList = new ArrayList<NameValuePair>();
10
11 BasicNameValuePair bPair1 = new BasicNameValuePair("method", "loginMobile");
12 BasicNameValuePair bPair2 = new BasicNameValuePair("loginname", "abc");
13 BasicNameValuePair bPair3 = new BasicNameValuePair("password", "abc");
14 pairsList.add(bPair1);
15 pairsList.add(bPair2);
16 pairsList.add(bPair3);
17 HttpEntity httpEntity = null;
18 try {
19 //参数转码
20 httpEntity = new UrlEncodedFormEntity(pairsList,"utf-8");
21 //请求实体放入post请求中
22 httpPost.setEntity(httpEntity);
23 //用http连接执行post请求并且获得http请求响应
24 HttpResponse response = httpClient.execute(httpPost);
25 //从response中取到响应实体
26 HttpEntity entity = response.getEntity();
27 //把响应实体转到文本
28 String html = EntityUtils.toString(entity);
29 System.out.println("====="+html);
30 } catch (UnsupportedEncodingException e) {
31 // TODO Auto-generated catch block
32 e.printStackTrace();
33 }
34 }
35
36 }

最新文章

  1. 升级 Visual Studio 2015 CTP 5 的坑、坑、坑
  2. JS-安全检测JavaScript基本数据类型和内置对象的方法
  3. 【转】ContextLoaderListener 和 DispatcherServlet
  4. 解决webApi&lt;Message&gt;An error has occurred.&lt;/Message&gt;不能写多个Get方法的问题
  5. 【数据库摘要】5_Sql_IN
  6. OvS: data structure analysis
  7. HCSR04超声波传感器驱动
  8. Identity Service - 解析微软微服务架构eShopOnContainers(二)
  9. 为测试赋能,腾讯WeTest探索手游AI自动化测试之路
  10. 201521123024《Java程序设计》第7周学习总结
  11. git笔记------自己学习git的心得
  12. 在HyperLedger Fabric中启用CouchDB作为State Database
  13. JavaScript常见案例
  14. Lock接口
  15. FastDFS_v4.06+nginx-1.4.2配置详解
  16. Eclipse 快速提取一个方法 (重构)
  17. 【Kryo】简单地使用Kryo
  18. leetcode303
  19. ASP.NET 4(和ASP.NET MVC 2)中输出HTML编码的新语法&lt;%:%&gt;
  20. FocusBI:地产分析&amp;雪花模型

热门文章

  1. sonar-scanner命令参数分析
  2. 关于URL encode和parse
  3. DeDeCMS v5.7 漏洞复现
  4. P3971-[TJOI2014]Alice and Bob【贪心】
  5. AT2164-[AGC006C]Rabbit Exercise【差分,倍增,数学期望】
  6. HCNP Routing&amp;Switching之路由引入导致的问题及解决方案
  7. Mac里存储空间不足,该怎么删垃圾数据?
  8. 如何基于Jupyter notebook搭建Spark集群开发环境
  9. 微信公众号开发之H5页面跳转到指定的小程序
  10. 题解 [NOI2019]弹跳