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 ...
随机推荐
- MTIM(中间人攻击)
所谓的MITM攻击就是通过拦截正常的网络通信数据,并进行数据篡改和嗅探,而通信的双方却毫不知情. 信息篡改 当主机A.和主机B通信时,都由主机C来为其“转发”,如图一,而A.B之间并没有真正意思上的直 ...
- ANg-梯度下降算法
概念 为了解决线性回归问题,我们也用梯度下降算法. 算法逻辑如下: 对于线性回归模型中例子,梯度下降可以如下: 算法 实际上梯度下降可有通过求导.这里的符号":="是赋值的含义 有 ...
- RTX二次开发SDK需要注意的地方
1.如果是ASP.net二次开发调用的,线程池必须开发 Enable 32-bit Applications设置为true. 因为RTX的调用接口很多是32位的.否则会报一下错误 RootObj = ...
- 吴裕雄 14-MySQL DELETE 语句
以下是 SQL DELETE 语句从 MySQL 数据表中删除数据的通用语法:DELETE FROM table_name [WHERE Clause]如果没有指定 WHERE 子句,MySQL 表中 ...
- 关于ubuntu14.04,忘记root密码解决方案(经测试,内核3.13和4.2可行)。
网上已经有很多方案啦,我这里就不画蛇添足,直接上链接: http://jingyan.baidu.com/article/c843ea0b9e851077931e4aea.html 注意,有些机子没有 ...
- 网络传输中利用fastjson将复杂嵌套数据类型Json格式转换(GeoJsonPolygon)
如果一个对象太复杂了,那么在网络传输键的JSON格式数据转换容易出问题. 比如下面一个类Area.java import lombok.AllArgsConstructor; import lombo ...
- 使用AJAX实现文件上传时Illegal invocation错误
在参数里面加 processData:false, 就行了
- MATLAB GUI图片添加背景
global im [filename,pathname]=uigetfile('*.jpg','输入图片'); file=strcat(pathname,filename); im=imread(f ...
- metasploit framework(三):exploit模块
exploit模块 分为主动,被动exploit 主动exploit:攻击者(通常是客户端)主动发起连接请求,然后发送exploit给被攻击者(通常是服务器端) 被动exploit:被攻击者(通常是客 ...
- web.xml中<security-constraint>和四种认证类型
https://blog.csdn.net/lisheng19870305/article/details/40819481 sztc开发中http请求一直发送后无响应,通过soapui定位异常信息为 ...