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. 嵌入式02 STM32 实验02 端口输入输出各4种模式

    GPIO(General-purpose input/output 通用目的输入/输出端口) 电压(A模拟量)与电平(D数字量) GPIO 8种工作模式(输入四种.输出四种) 1.GPIO_Mode_ ...

  2. 11 IO流(八)——装饰器设计模式,Filter装饰流

    声明:本文部分图片及内容引用自:https://www.cnblogs.com/qiumingcheng/p/5219631.html java装饰器设计模式 举一个形象的例子,人可以说话,而扩音器可 ...

  3. AVR单片机教程——拨动开关

    在按键的上方有4个拨动开关.开关与按键,在原理和使用方法上都是很类似的,但有不同的用途——按键按下后松开就会弹起,而开关可以保存其状态. <switch.h> 定义了与开关相关的函数.sw ...

  4. gensim快速上手教程

    1 gensim是什么?        gensim是一个Python常用的的自然语言处理开发包, 主要用于词向量训练和加载词向量,以下解释其正确使用姿势. 2 正确使用姿势 from gensim. ...

  5. mybatis 多个中间表查询映射

    最近项目用到中间表,则遇到如何联查映射的问题,之前一直都是一个表头,多个明细或者一对一这样的关系,没遇到这样的问题,所以趁机找了下资料解决了这个问题. 表结构设计如下: 主表: CREATE TABL ...

  6. Java 枚举和抽象类添加状态值

    枚举: public enum CourseTypeEnum { VIDEO_COURSE(1,"录制课程"), LIVE_COURSE(2,"直播课程"), ...

  7. Java8 基础数据类型包装类-Long

     https://blog.csdn.net/u012562117/article/details/79023440 基础 //final修饰不可更改,每次赋值都是新建类(其中-128~127是通过L ...

  8. 14-4 ADO.NET简介

    微软数据访问方式历史阶段 ①ODBC(Open Database Connectivity)是第一个使用SQL访问不同关系数据库的访问技术.使用ODBC应用程序能够通过单一的命令操作不同的数据库,而开 ...

  9. Java字节流文件复制

    1.字节流 在 Java 中,文件的复制使用字节输入流和字节输出流实现,java.io 包有 InputStream 和 OutputStream 这两个顶层抽象类规范了读写文件所需的核心 API. ...

  10. 机器码-字节码-CLR-JIT-托管代码-非托管代码-unsafe-GC-fixed

    0. 机器码 直接由机器码对应平台的CPU执行的指令集, 因此无法在其他指令集的CPU上运行. 无法跨平台. 由本地代码编译得到. (托管代码通过JIT生成) 1. 字节码 即 bytecode 是一 ...