httpclient的简单使用
1、通过get请求后台,注意tomcat的编码设置成utf-8; <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" />
/**
* 发送 get请求
*/
public static void get() {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//先将参数放入List,再对参数进行URL编码
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("get", "get请求哈哈哈")); //对参数编码
String param = URLEncodedUtils.format(params, "UTF-8");
// 创建httpget.
HttpGet httpget = new HttpGet("http://localhost:8080/HttpServleTest.html?"+param); // 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 获取响应实体
HttpEntity entity = response.getEntity();
// 打印响应状态码
System.out.println(response.getStatusLine().getStatusCode());
if (entity != null) {
// 打印响应内容
System.out.println("Response content: " + EntityUtils.toString(entity,"UTF-8"));
}
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.post请求
/**
* 发送 post
*/
public static void post() {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost("http://localhost:8080/HttpServleTest.html");
// 创建参数队列
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("post", "post请求哈哈哈"));
UrlEncodedFormEntity uefEntity;
try {
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
}
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、后台服务程序和本案例代码下载地址:http://download.csdn.net/download/u013865056/9971496
httpclient的简单使用的更多相关文章
- HttpURLConnection和HttpClient的简单用法
HttpURLConnection的简单用法:先通过一个URL创建一个conn对象,然后就是可以设置get或者是post方法,接着用流来读取响应结果即可 String html = null; lon ...
- 14 微服务电商【黑马乐优商城】:day02-springcloud(理论篇一:HttpClient的简单使用)
本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一: ...
- 基于Netty4的HttpServer和HttpClient的简单实现
Netty的主页:http://netty.io/index.html 使用的Netty的版本:netty-4.0.23.Final.tar.bz2 ‐ 15-Aug-2014 (Stable, Re ...
- Android 中HttpURLConnection与HttpClient的简单使用
1:HttpHelper.java public class HttpHelper { //1:标准的Java接口 public static String getStringFromNet1(Str ...
- (高级篇 Netty多协议开发和应用)第十章-Http协议开发应用(基于Netty的HttpServer和HttpClient的简单实现)
1.HttpServer package nettyHttpTest; import io.netty.bootstrap.ServerBootstrap; import io.netty.chann ...
- NetCore控制台程序-使用HostService和HttpClient实现简单的定时爬虫
.NetCore承载系统 .NetCore的承载系统, 可以将长时间运行的服务承载于托管进程中, AspNetCore应用其实就是一个长时间运行的服务, 启动AspNetCore应用后, 它就会监听网 ...
- HttpClient 之Fluent API 简单使用
相比于HttpClient 之前的版本,HttpClient 4.2 提供了一组基于流接口(fluent interface)概念的更易使用的API,即Fluent API. 为了方便使用,Fluen ...
- HttpClient4.5简单使用
一.HttpClient简介 HttpClient是一个客户端的HTTP通信实现库,它不是一个浏览器.关于HTTP协议,可以搜索相关的资料.它设计的目的是发送与接收HTTP报文.它不会执行嵌入在页面中 ...
- 模拟登陆CSDN——就是这么简单
工具介绍 本篇文章主要是解说怎样模拟登陆CSDN.使用的工具是HttpClient+Jsoup 当中HttpClient主要是负责发送请求,而Jsoup主要是解析HTML 你可能对HttpClient ...
随机推荐
- pgsql restart
/etc/init.d/postgresql restart
- Dockerfile构建MySQL
转自:https://www.cnblogs.com/jsonhc/p/7807931.html 利用Dockerfile自定义构建MySQL服务折腾了几天,一直在启动服务上出现错误,现在终于解决了该 ...
- haproxy + nginx + proxy protocol 获得客户真实IP方法
公司网站架构为: 前面2台HA负载均衡,后面3台Nginx负载均衡反向代理,然后后面有N台WEB服务器 由于要统计IP,需要在WEB服务器日志里体现客户端真实IP 那么问题来了,通过HA代理的HTTP ...
- Java动态代理的两种实现方法
注:文章转载自:https://blog.csdn.net/m0_38039437/article/details/77970633 一.代理的概念 动态代理技术是整个java技术中最重要的一个技术, ...
- web前端基础知识!
[HTML文档的基本结构和语法][基本结构]: <HTML> HTML 文件开始 <HEAD> HTML 文件的头部开始 <title> 网页的标题</tit ...
- Hadoop集群(二) HDFS搭建
HDFS只是Hadoop最基本的一个服务,很多其他服务,都是基于HDFS展开的.所以部署一个HDFS集群,是很核心的一个动作,也是大数据平台的开始. 安装Hadoop集群,首先需要有Zookeeper ...
- python错题整理
1.列表list去重 l1 = [1,1,2,3,5,5,4,4,4,5,6] set1 = set(l1) # print(set1) # set是集合 l2 = list(set1) # 将集合转 ...
- MATLAB总结二
1.如何将一个多项式中的系数按照幂次提取出来? sym2poly(ans) %ans为一个多项式. 2.在写第二份模式识别的大作业时遇到如下困难: 每个样本贡献给总概率的是一个关于x的表达式,我将所有 ...
- Numpy知识(二)
ndarray的简单数学计算就和普通的a+b,a-b,a*b,a/b等类似. 关于ndarray的切片: arr[n]:寻找第n个元素(针对一维)arr[n:m]:从下标为n元素开始,截取到下标为m- ...
- hibernate ID
一:主键生成策略大体分类: 1:hibernate 负责对主键ID赋值 2:应用程序自己为主键ID赋值(不推荐使用) 3:底层数据库为主键ID赋值 二:具体用法 ...