淘淘商城之httpclient
一、什么是httpclient
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议
来访问网络资源;虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,
JDK 库本身提供的功能还不够丰富和灵活;HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新
的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议
下载地址:http://hc.apache.org/
二、功能介绍
以下列出的是 HttpClient 提供的主要的功能
(1)实现了所有 HTTP 的方法
(2)支持自动转向
(3)支持 HTTPS 协议
(4)支持代理服务器等
要知道更多详细的功能可以参见 HttpClient 的主页
三、导入依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
四、用法示例
(1)执行get请求
public class DoGET {
public static void main(String[] args) throws Exception {
//创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//创建http GET请求
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
CloseableHttpResponse response = null;
try {
//执行请求
response = httpclient.execute(httpGet);
//判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("内容长度:" + content.length());
//FileUtils.writeStringToFile(new File("C:\\baidu.html"), content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
}
(2)执行带参数的get请求
public class DoGETParam {
public static void main(String[] args) throws Exception {
//创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//定义请求的参数
URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();
System.out.println(uri);
//创建http GET请求
HttpGet httpGet = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
//执行请求
response = httpclient.execute(httpGet);
//判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
}
(3)执行post请求
public class DoPOST {
public static void main(String[] args) throws Exception {
//创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//创建http POST请求
HttpPost httpPost = new HttpPost("http://www.oschina.net/");
CloseableHttpResponse response = null;
try {
//执行请求
response = httpclient.execute(httpPost);
//判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
}
(4)执行带参数的post请求
public class DoPOSTParam {
public static void main(String[] args) throws Exception {
//创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//创建http POST请求
HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
//设置2个post参数,一个是scope、一个是q
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
parameters.add(new BasicNameValuePair("scope", "project"));
parameters.add(new BasicNameValuePair("q", "java"));
//构造一个form表单式的实体
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
//将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
CloseableHttpResponse response = null;
try {
//执行请求
response = httpclient.execute(httpPost);
//判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}
}
(5)封装HttpClient通用工具类
public class HttpClientUtil {
public static String doGet(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doPost(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建参数列表
if (param != null) {
List<NameValuePair> paramList = new ArrayList<>();
for (String key : param.keySet()) {
paramList.add(new BasicNameValuePair(key, param.get(key)));
}
// 模拟表单
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
httpPost.setEntity(entity);
}
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
public static String doPost(String url) {
return doPost(url, null);
}
}
淘淘商城之httpclient的更多相关文章
- 001淘淘商城项目:项目的Maven工程搭建
开始一个新的项目,特此记录,资料全部来源于传智播客,感谢. 我们要做一个类似电商的项目.用maven做管理. maven里面主要分为三种工程: 1:pom工程:用在父级工程,聚合工程中 2:war工程 ...
- 淘淘商城_day01_课堂笔记
今日大纲 聊聊电商行业 电商行业发展 11.11 2015双11: 2016年: 预测:2017年的双11交易额将达到:1400亿 电商行业技术特点 淘淘商城简介 淘淘商城的前身 电商行业的概念 B2 ...
- 【原】从零开始改造淘淘商城(引入dubbo解决项目耦合)02
前言: 关于为什么要引入dubbo框架,而不是用spring cloud或者是motan呢,主要是笔者现在公司用的就是dubbo,并且第一次接触到微服务的概念是来源于dubbo,再加上最近dubbo频 ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第一天
文章大纲 一.课程介绍二.淘淘商城基本介绍三.后台管理系统工程结构与搭建四.svn代码管理五.项目源码与资料下载六.参考文章 一.课程介绍 1. 课程大纲 一共14天课程(1)第一天:电商行业的背 ...
- JAVAEE——淘淘商城第一天:电商行业的背景和技术特点,商城的介绍、技术的选型、系统架构和工程搭建
1. 学习计划 1.电商行业的背景. 2.电商行业的技术特点 3.商城的介绍 a) 常用的名词介绍 b) 系统功能介绍 4.淘淘商城的系统架构 a) 传统架构 b) 分布式架构 c) 基于服务的架构 ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第十四天(非原创)
文章大纲 一.淘淘商城总体架构介绍二.淘淘商城重要技术点总结三.项目常见面试题四.项目学习(all)资源下载五.参考文章 一.淘淘商城总体架构介绍 1. 功能架构 2. 技术选型 (1)Sprin ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第六天(非原创)
文章大纲 一.课程介绍二.今日内容简单介绍三.Httpclient介绍与实战四.项目源码与资料下载五.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工 ...
- (转)淘淘商城系列——使用maven构建工程
http://blog.csdn.net/yerenyuan_pku/article/details/72669269 开发工具和环境 这里,我统一规范一下淘淘商城的开发工具和环境,如下: Eclip ...
- day68_淘淘商城项目_01
原文:day68_淘淘商城项目_01 课程计划 第一天: 1.电商行业的背景介绍--电子商务 2.淘淘商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建--后台工程 a) 使用maven搭建 ...
- day68_淘淘商城项目_01_电商介绍 + 互联网术语 + SOA + 分布式 + 集群介绍 + 环境配置 + 框架搭建_匠心笔记
课程计划 第一天: 1.电商行业的背景介绍--电子商务 2.淘淘商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建--后台工程 a) 使用maven搭建工程(工程大) b) 使用maven的 ...
随机推荐
- jsp中的javascript的$(document).ready( function() { $("#loginForm").validate()
转自:https://bbs.csdn.net/topics/392459787?list=71147533 下面是jsp页面中的JavaScript代码 $(document).ready( fun ...
- 三、统一威胁管理(UTM)
简介 统一威胁管理(Unified Threat Management),简称UTM. 2004年9月,IDC首度提出“统一威胁管理”的概念,即将防病毒.入侵检测和防火墙安全设备划归统一威胁管理(Un ...
- java 判断数据是否为空
/** * 方法描述:自定义判断是否为空 * 创建作者:李兴武 * 创建日期:2017-06-22 19:50:01 * * @param str the str * @return the bool ...
- Dockers的安装
添加yum源 #下载163的yum源到本地 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7 ...
- DVWA全级别之Insecure CAPTCHA(不安全的验证码)
Insecure CAPTCHA Insecure CAPTCHA,意思是不安全的验证码,CAPTCHA是Completely Automated Public Turing Test to Tell ...
- beego 页面布局
模板 this.Layout = "admin/layout.html" this.TplName = "admin/list.html" 在layout.ht ...
- Apache Kafka(九)- Kafka Consumer 消费行为
1. Poll Messages 在Kafka Consumer 中消费messages时,使用的是poll模型,也就是主动去Kafka端取数据.其他消息管道也有的是push模型,也就是服务端向con ...
- 题解【洛谷P5315】头像上传
本题就是按照题目模拟, 只是要注意一些细节问题. Wrong Answer的主要有以下2个问题: 注意这句话: 在图片上传前,系统会对图片进行如下处理:如果图片的任何一边长度超过了 G ,那么系统会不 ...
- easyui的combogrid
easyui的combogri下拉框用在项目中很多,有时会出现很多问题,当然也好解决. 1.当向后台传id值时,用户输入的与查询出来的显示值一样,但combogrid为空? 情景:输入‘李四’,和显示 ...
- Django_模型
1. ORM 2. 简单使用 3. 外键 2.0以上的版本要这样写s_grade = models.ForeignKey(Grade,on_delete=models.CASCADE) 3. 修改表名 ...