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. [转帖]央行推出数字货币DCEP:基于区块链技术、将取代现钞

    央行推出数字货币DCEP:基于区块链技术.将取代现钞 天天快报的内容. 密码财经 2019-10-29 18:15 关注   前不久的10月23日,Facebook的首席执行官扎克伯格在美国国会听证会 ...

  2. Linux中的13个基本Cat命令示例

    cat(“ concatenate ”的缩写)命令是Linux / Unix等操作系统中最常用的命令之一.cat命令允许我们创建单个或多个文件,查看文件包含,连接文件以及在终端或文件中重定向输出.在本 ...

  3. 47 容器(六)——HashMap

    HashMap的概念 HashMap底层实现了哈希表,这是一种非常重要的数据结构,对于以后我们理解很多技术都有帮助,例如 redis数据库的核心技术和HashMap一样,因此,非常有必要让大家理解. ...

  4. XGBoost对波士顿房价进行预测

    import numpy as np import matplotlib as mpl mpl.rcParams["font.sans-serif"] = ["SimHe ...

  5. Elasticsearch-6.7.0系列(六)ES设置集群密码

    感谢此老兄:<手把手教你搭建一个 Elasticsearch 集群> 前提准备 安装kibana-6.7.0: <Elasticsearch-6.7.0系列(三)5601端口 kib ...

  6. [LOJ3048] [十二省联考2019] 异或粽子

    题目链接 LOJ:https://loj.ac/problem/3048 洛谷:https://www.luogu.org/problemnew/show/P5283 Solution 考虑每个子串都 ...

  7. PDA日常问题

    一.连接网络异常 1.摩托摩拉3190连接wifi时报错,提示:scan error adapter unavailable 确认网卡是不是禁用状态,CE是右下角有个蓝色的图,上面有个X,点一下,然后 ...

  8. 【转载】C#中string类使用Substring方法截取字符串

    在C#的字符串操作过程中,截取字符串是一种常见的字符串操作,可使用string类的Substring方法来完成字符串的截取操作,该方法支持设定截取的开始位置以及截取的字符串长度等参数,Substrin ...

  9. Django:必会ORM查

    1.ORM常用字段 在models.py,创建表中字段 1.1AutoField 自增的整形字段,必填参数primary_key=True,则成为数据库的主键.无该字段时,django自动创建.一个m ...

  10. viewer 图片点击放大 用法汇总

    A 不用viewer插件 1弹出框 https://www.cnblogs.com/web1/p/8989967.html 2表格中 https://www.jianshu.com/p/c17f4f6 ...