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的简单使用的更多相关文章

  1. HttpURLConnection和HttpClient的简单用法

    HttpURLConnection的简单用法:先通过一个URL创建一个conn对象,然后就是可以设置get或者是post方法,接着用流来读取响应结果即可 String html = null; lon ...

  2. 14 微服务电商【黑马乐优商城】:day02-springcloud(理论篇一:HttpClient的简单使用)

    本项目的笔记和资料的Download,请点击这一句话自行获取. day01-springboot(理论篇) :day01-springboot(实践篇) day02-springcloud(理论篇一: ...

  3. 基于Netty4的HttpServer和HttpClient的简单实现

    Netty的主页:http://netty.io/index.html 使用的Netty的版本:netty-4.0.23.Final.tar.bz2 ‐ 15-Aug-2014 (Stable, Re ...

  4. Android 中HttpURLConnection与HttpClient的简单使用

    1:HttpHelper.java public class HttpHelper { //1:标准的Java接口 public static String getStringFromNet1(Str ...

  5. (高级篇 Netty多协议开发和应用)第十章-Http协议开发应用(基于Netty的HttpServer和HttpClient的简单实现)

    1.HttpServer package nettyHttpTest; import io.netty.bootstrap.ServerBootstrap; import io.netty.chann ...

  6. NetCore控制台程序-使用HostService和HttpClient实现简单的定时爬虫

    .NetCore承载系统 .NetCore的承载系统, 可以将长时间运行的服务承载于托管进程中, AspNetCore应用其实就是一个长时间运行的服务, 启动AspNetCore应用后, 它就会监听网 ...

  7. HttpClient 之Fluent API 简单使用

    相比于HttpClient 之前的版本,HttpClient 4.2 提供了一组基于流接口(fluent interface)概念的更易使用的API,即Fluent API. 为了方便使用,Fluen ...

  8. HttpClient4.5简单使用

    一.HttpClient简介 HttpClient是一个客户端的HTTP通信实现库,它不是一个浏览器.关于HTTP协议,可以搜索相关的资料.它设计的目的是发送与接收HTTP报文.它不会执行嵌入在页面中 ...

  9. 模拟登陆CSDN——就是这么简单

    工具介绍 本篇文章主要是解说怎样模拟登陆CSDN.使用的工具是HttpClient+Jsoup 当中HttpClient主要是负责发送请求,而Jsoup主要是解析HTML 你可能对HttpClient ...

随机推荐

  1. BerOS File Suggestion(stl-map应用)

    Polycarp is working on a new operating system called BerOS. He asks you to help with implementation ...

  2. Redis学习笔记一(Redis的详细安装及Linux环境变量配置和启动)

     Redis Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 我使用的是下面这个版本: 若没有资源的话,我在 ...

  3. mycat的schema.xml的个人的一点理解

    官方文档里讲的详细的部分的我就不再赘述了,我只是谈谈我自己的理解 刚开始接触mycat,最重要的几个配置文件有server.xml,schema.xml,还有个rule.xml配置文件 具体都是干啥用 ...

  4. numpy.distutils.system_info.NotFoundError: no lapack/blas resources found问题解决

    操作环境 Python3.6 + Windows7 问题现象   利用pip自动安装seaborn/numpy/scipy(pip install seaborn)模块失败,提示numpy.distu ...

  5. CrazySNS has on line - And you'll see why 1984 won't be like "1984."

    On May 10th, CrazySNS has on line , And you will see why 1984 won’t be like "1984."

  6. Delphi动态配置ODBC数据源--SQL Server版本

    (摘自)http://jxlearnew.blog.163.com/blog/static/549786592007102451431413/ 这里介绍一种用Delphi来实现动态注册的方法,希望对各 ...

  7. 使用RestTemplate调用接口上传文件

    场景 接口接受一个文件,缓存在本地,验证文件的完整性及内容,然后将文件上传至云服务器: 下面只写利用RestTemplate将文件上传至云服务器,至于文件上传以及缓存在本地可以参考:JAVA文件上传: ...

  8. native和webview切换

    native和webview 标签(空格分隔): native和webview 现在目前大部分的app都是native和webview混合,对应的native上的元素可以通过uiautomatorvi ...

  9. yum update 自动忽略内核更新

    系统每天凌晨 3 点自动执行 yum update 任务 但升级内核后,会出现下面情况 一些编译软件需要内核模块才能够被调用, 而内核模块需要与当前版本内核编译后才能够使用, 假设内核升级后,之前软件 ...

  10. 五、Singleton 单例模式

    需求:保证对象只创建一次 说明: 分为懒汉式.饿汉式,通过是否一开始就创建静态对象.饿汉式需要考虑线程并发的安全 懒汉式: public class Singleton { private stati ...