HTTPClients使用
一. 创建httpclient对象用于发送get、post等请求
1. HttpClients.custom()的方式--自定义httpclient对象,多用于含有cookie的请求
1. CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("session","true");
cookie.setDomain("127.0.0.1");
cookie.setPath("/");
cookieStore.addCookie(cookie);
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
2. HttpClients.createDefault()的方式--默认httpclient方式
CloseableHttpClient httpClient = HttpClients.createDefault();
二. 创建请求对象
1. Get请求:HttpGet get= new HttpGet("http://127.0.0.1:8899/getNoparams");
2. Post请求:HttpPost post = new HttpPost("http://127.0.0.1:8899/postNoparams");
3. 设置请求数据
1. form表单格式(Bean对象转Map,遍历Map添加到List集合中)
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name","zhangsan");
hashMap.put("age","18");
List<NameValuePair> list = new ArrayList<NameValuePair>();
Set<Map.Entry<String, String>> entries = hashMap.entrySet();
for(Map.Entry<String, String> entry:entries){
list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
post.setEntity(new UrlEncodedFormEntity(list,"utf-8"));
2. json格式(Bean对象转JSON字符串)
1. User user = new User();
user.setName("zhangsan");
user.setAge("18");
String json = JSONObject.toJSON(user).toString();
post.setEntity(new StringEntity(json));
三. 发送请求 返回值类型为CloseableHttpResponse
CloseableHttpResponse response = httpclient.execute(post)
CloseableHttpResponse response = httpclient.execute(get)
四. 返回值转为字符串
String data = EntityUtils.toString(response.getEntity());
HTTPClients使用的更多相关文章
- URLConnection 和 HttpClients 发送请求范例
. java.net.URLConnection package test; import java.io.BufferedReader; import java.io.IOException; im ...
- java后台訪问url连接——HttpClients
java后台訪问url,并传递数据--通过httpclient方式 须要的包,包可能多几个额外的,假设无用或者冲突删除就可以.httpclient是使用的是4.4.1的版本号:http://downl ...
- Java 实现 HttpClients+jsoup,Jsoup,htmlunit,Headless Chrome 爬虫抓取数据
最近整理一下手头上搞过的一些爬虫,有HttpClients+jsoup,Jsoup,htmlunit,HeadlessChrome 一,HttpClients+jsoup,这是第一代比较low,很快就 ...
- HttpClients+Jsoup抓取笔趣阁小说,并保存到本地TXT文件
前言 首先先介绍一下Jsoup:(摘自官网) jsoup is a Java library for working with real-world HTML. It provides a very ...
- URLConnection 和 HttpClients 发送请求范例【原】
笔记,未完全标准. java.net.URLConnection package test; import java.io.BufferedReader; import java.io.IOExcep ...
- 我的HttpClients工具
import java.io.IOException; import javax.ws.rs.core.MediaType; import org.apache.commons.httpclient. ...
- HttpClient的替代者 - RestTemplate
需要的包 ,除了Spring的基础包外还用到json的包,这里的数据传输使用json格式 客户端和服务端都用到一下的包 <!-- Spring --> <dependency> ...
- java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INSTANCE
Android发出HTTP请求时出现了这个错误: java.lang.NoSuchFieldError: org.apache.http.message.BasicLineFormatter.INST ...
- java https单向认证(忽略认证)并支持http基本认证
https单向认证(忽略认证)并支持http基本认证, 温馨提示 1,jar包要导入对 2,有匿名类编译要注意 3,欢迎提问,拿走不谢!背景知识 Https访问的相关知识中,主要分为单向验证和双向验证 ...
- [Java]使用HttpClient实现一个简单爬虫,抓取煎蛋妹子图
第一篇文章,就从一个简单爬虫开始吧. 这只虫子的功能很简单,抓取到”煎蛋网xxoo”网页(http://jandan.net/ooxx/page-1537),解析出其中的妹子图,保存至本地. 先放结果 ...
随机推荐
- 解决Avalonia 11.X版本的中文字体问题
网上搜索的方法使用接口"IFontManagerImpl"这个方法目前只能用于Avalonia 10.X版本,因为11版本后官方把这个接口的成员都设置成了非plubic,所以之前的 ...
- Mybatis框架的搭建和基本使用
本文总结最原始Mybatis框架的搭建和最基本使用(不涉及Spring框架体系). 1 依赖 首先,我们要引入Mybatis依赖: <dependency> <groupId> ...
- Linux校验文件MD5和SHA值的方法
1.需求背景 下载或传输文件后,需要计算文件的MD5.SHA256等校验值,以确保下载或传输后的文件和源文件一致 2.校验方法 如上图所示,可以使用Linux自带的校验命令来计算一个文件的校验值 Li ...
- 2016A
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm& ...
- mount时候遇到写保护,将以只读方式挂载
mount时候遇到写保护,将以只读方式挂载 遇到 mount: 未知的文件系统类型"(null)" [root@localhost ~]# mount /dev/sdb /mnt/ ...
- Spring Boot 目录遍历--表达式注入--代码执行--(CVE-2021-21234)&&(CVE-2022-22963)&&(CVE-2022-22947)&&(CVE-2022-2296)
Spring Boot 目录遍历--表达式注入--代码执行--(CVE-2021-21234)&&(CVE-2022-22963)&&(CVE-2022-22947)& ...
- CAS中ABA问题的解决
转自(here) CAS问题的产生 在运用CAS做Lock-Free操作中有一个经典的ABA问题: 线程1准备用CAS将变量的值由A替换为B,在此之前,线程2将变量的值由A替换为C,又由C替换为A ...
- fprintf
fprintf 是一个标准C库函数,用于将格式化的输出写入到指定文件流中.它的函数原型如下: int fprintf(FILE *stream, const char *format, ...); 参 ...
- Go 多版本管理工具
Go 多版本管理工具 目录 Go 多版本管理工具 一.go get 命令 1.1 使用方法: 二.Goenv 三.GVM (Go Version Manager) 四.voidint/g 4.1 安装 ...
- jdk-14.0.1环境搭建及cmd环境编译执行
1.安装包获取 https://www.oracle.com/java/technologies/javase/jdk14-archive-downloads.html 2.环境变量配置 最新版本的J ...