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 ...
随机推荐
- Python : 什么是*args和**kwargs
让生活Web个够 先来看个例子: def foo(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '-- ...
- 2018SDIBT_国庆个人第一场
A - Turn the Rectangles CodeForces - 1008B There are nn rectangles in a row. You can either turn eac ...
- java 中 Integer 传参方式的问题
Java本身都是值传递式的调用,对于对象传递的是地址值.给地址值重新赋值等于重新指向,不会影响外层. 而且这里Integer对象也有特殊性.其实现上可能类似 class Integer{ final ...
- Windows下查看端口常用命令以及关闭端口的方法
1.C:\Users\JavaKam> netstat -ano|findstr 1099 TCP LISTENING TCP [::]: [::]: LISTENING 2.出现: C:\Us ...
- 获取jdk支持的编码类型
//获取jdk支持的编码类型 Map<String,Charset> maps = Charset.availableCharsets(); for(Map.Entry<String ...
- Jsp基本语法 第二节
关于JSP的声明 即在JSP页面定义方法或者变量: <%!Java代码%> 在JSP页面中执行的表达式:<%=表达式%> 这个里尤其注意不能以:结束 JSP页面生命周期 ...
- C++ CTreeview的checkbox使用方法
1. 消息事件 (1)鼠标点击当前ITEM的CHECKBOX:引发NM_CLICK事件并传递TVHT_ONITEMSTATEICON. (2)鼠标点击当前ITEM的TEXT:引发NM_CLIC ...
- C:\\MFC控件大小随窗体大小而改变
一.大小和位置都改变 1.首先为窗体类添加CRect m_rect,该成员变量用来记录窗体的当前大小. 2.在类向导里面(Ctrl+W),为窗体添加消息WM_SIZE的响应函数OnSize(): 注意 ...
- Pandas分类
Pandas分类 categorical data是指分类数据:数据类型为:男女.班级(一班.二班).省份(河北.江苏等),若使用赋值法给变量赋值,例如(男=1,女=0),数字1,0之间没有大小之分, ...
- 学JS的心路历程 -物件与原型(二)
昨天有提到说Object.setPrototypeOf可以指定一个物件为另一个物件的原型,但有想过到底这个原型,也就是[[Prototype]]最终会到何处吗? 答案是Object.prototype ...