httpclient工具使用(org.apache.httpcomponents.httpclient)

引入依赖

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>

  

get请求

public static void main(String[] args) throws Exception {

		//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建Http get请求
HttpGet httpGet = new HttpGet("http://www.xxx.com/rest/content?categoryId=4&page=1&rows=20");
//接收返回值
CloseableHttpResponse response = null; try {
//请求执行
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("--------->" + content);
}
}finally{
if(response!=null){
response.close();
}
httpClient.close();
}

  

get带参数请求

public static void main(String[] args) throws Exception{

		//创建httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//定义http get参数
URI uri = new URIBuilder("http://www.xxxx.com/rest/content").setParameter("categoryId", "4")
.setParameter("page", "1").setParameter("rows", "30").build();
System.out.println(uri);
//创建http get请求
HttpGet httpGet = new HttpGet(uri); //接受请求返回的定义
CloseableHttpResponse response = null;
try {
//执行get请求
response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(content);
}
}finally{
if(response!=null){
response.close();
}
httpClient.close();
}
}

  

http post请求

 public static void main(String[] args) throws Exception {

        // 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建http POST请求
HttpPost httpPost = new HttpPost("http://www.oschina.net/");
// 伪装成浏览器
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"); CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpPost);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
} }

  

http post带参数请求

public static void main(String[] args) throws Exception{
//创建httpclient
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建http post
HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
//模拟浏览器设置头
httpPost.setHeader(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"); //设置请求数据
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("scope", "project"));
params.add(new BasicNameValuePair("q", "java"));
params.add(new BasicNameValuePair("fromerr", "7nXH76r7"));
//构建表单
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params);
//将表达请求放入到httpost
httpPost.setEntity(formEntity); //返回类型
CloseableHttpResponse response = null; try {
response = httpClient.execute(httpPost);
String content = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(content);
}finally{
if(response!=null){
response.close();
}
httpClient.close();
} }

  

httpclient工具使用(org.apache.httpcomponents.httpclient)的更多相关文章

  1. org.apache.httpcomponents.httpclient

    apache org doc :http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e49 ...

  2. org.apache.httpcomponents:httpclient 工具类

    基于httpclient 版本4.4.1 因为http连接需要三次握手,在需要频繁调用时浪费资源和时间 故采用连接池的方式连接 根据实际需要更改  连接池最大连接数.路由最大连接数 另一个需要注意的是 ...

  3. org.apache.httpcomponents httpclient 发起HTTP JSON请求

    1. pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactI ...

  4. org.apache.commons.httpclient工具类

    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpcl ...

  5. org.apache.commons.httpclient工具类(封装的HttpUtil)

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...

  6. 【commons-httpclient】Java中HttpClient工具访问Web请求

    注意jar包是: HttpClient工具使用 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程 ...

  7. java apache commons HttpClient发送get和post请求的学习整理(转)

    文章转自:http://blog.csdn.net/ambitiontan/archive/2006/01/06/572171.aspx HttpClient 是我最近想研究的东西,以前想过的一些应用 ...

  8. Java网络编程:利用apache的HttpClient包进行http操作

    本文介绍如何利用apache的HttpClient包进行http操作,包括get操作和post操作. 一.下面的代码是对HttpClient包的封装,以便于更好的编写应用代码. import java ...

  9. org.apache.commons.httpclient.HttpClient的使用(转)

    HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 java net包中已经提供了访 ...

随机推荐

  1. dockfile构建自己的tomcat

    touch  Dockerfile 在Dockerfile中输入以下内容 FROM centosMAINTAINER Irish<3395327965@qq.com>#把宿主机当前上下文的 ...

  2. 打印机API

    转载 wangkuiyun 发布于2014-03-21 09:45:37 阅读数 4228 收藏 更新于2014-03-21 09:45:38     AbortDoc 取消一份文档的打印AbortP ...

  3. isset和empty,isset和unset,__isset和__unset

    1.isset() 用来检测变量是否存在,如果变量存在,且值不等于零,返回ture empty() 用来检测是否为空,如果变量值值为"".0."0".NULL. ...

  4. 升级nginx1.12为1.161版本

    升级nginx1.12为1.161版本 一.添加源 到 cd /etc/yum.repos.d/ 目录下 新建nginx.repo 文件 vim nginx.repo 输入以下信息 [nginx-st ...

  5. Django最全思维导图

    思维导图传送门

  6. Django模型层之ORM

    Django模型层之ORM操作 一 ORM简介 我们在使用Django框架开发web应用的过程中,不可避免地会涉及到数据的管理操作(如增.删.改.查),而一旦谈到数据的管理操作,就需要用到数据库管理软 ...

  7. python MySQL 插入Elasticsearch

    一.需求分析 注意: 本环境使用 elasticsearch 7.0版本开发,切勿低于此版本 mysql 表结构 有一张表,记录的数据特别的多,需要将7天前的记录,插入到Elasticsearch中, ...

  8. [LOJ2065] [SDOI2016]模式字符串

    题目链接 洛谷:https://www.luogu.org/problemnew/show/P4075 LOJ:https://loj.ac/problem/2065 Solution 这种题看起来就 ...

  9. ZYNQ block design警告:[BD 41-968] AXI interface port /axi_lite4 is not associated to any clock port. It may not work correctly.

    前言 在Block design中引出AXI接口给外部,检查设计告警如下: [BD 41-968] AXI interface port /axi_lite4 is not associated to ...

  10. HTML表格跨行、跨列操作(rowspan、colspan)

    转自:https://blog.csdn.net/u012724595/article/details/79401401 一般使用<td>元素的colspan属性来实现单元格跨列操作,使用 ...