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 ...
随机推荐
- $event Object angularjs
You can pass the $event object as an argument when calling the function. The $event object contains ...
- Structs复习 Structs标签
如果类型是object Structs会把它默认解析为OGNL表达式 想取字符串的话就 ‘’ ‘ ’ ‘’ jar包 web.xml. <?xml version="1.0" ...
- Hadoop集群(三) Hbase搭建
前面已经完成Zookeeper和HDFS的安装,本文会详细介绍Hbase的安装步骤.以及安装过程中遇到问题的汇总. 系列文章: Hadoop集群(一) Zookeeper搭建 Hadoop集群(二 ...
- macbook pro。已经连接上wifi,但是,不能上网的问题
有天,macbook pro关机后,再打开就上不了网了,后面网上看了,说是安装了 lantern出问题,我一想,有次关机lantern是被我强制关掉的.所以再次打开lantern就可以 上网了,然后正 ...
- CSS 字体术语
字体有些术语需要了解. 衬线字体 衬线字体指的是有衬线的字体,又称为有衬线体.衬线字.曲线描边字,俗称白体字. 衬线指的是字形笔画末端的装饰细节部分. 衬线字体便于阅读,通常用于长篇文本. 常见的衬线 ...
- crontab -e文件存放路径
crontab -e结果存放在/var/spool/cron/crontabs中
- beebase
1.简单介绍 BeeBase是一个在线生物信息学数据库,显示与Apis mellifera.欧洲蜜蜂以及一些病原体和其他物种有关的数据.它是与蜜蜂基因组测序联盟合作开发的.BeeBase是蜜蜂研究社区 ...
- modal template
<div class="modal fade" id="tmp_order_modal" tabindex="-1" role=&qu ...
- jquery不能实时获取CKEDITOR值的解决方法
不用传统的获取值的方法: var ckeditor = document.getElementById("ckeditor").value; 换成: var ckeditor = ...
- python--第三天总结
[collection系列]1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 c = Counter('abcdeabc ...