
httpclient获取url响应信息Demo
发布日期:2021-05-15 07:14:19
浏览次数:14
分类:精选文章
本文共 4596 字,大约阅读时间需要 15 分钟。
httpclient获取url响应信息Demo,两种方式:
pom.xml
4.0.0 dtwave_pro com.dtwave.pros 1.0-SNAPSHOT org.apache.httpcomponents httpclient 4.5.3 com.alibaba fastjson 1.2.24 org.apache.commons commons-lang3 3.8.1 org.apache.commons commons-io 1.3.2 org.apache.hive hive-exec 1.2.1 provided junit junit 3.8.1 test maven-clean-plugin 3.0.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.7.0 maven-surefire-plugin 2.20.1 maven-jar-plugin 3.0.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2 maven-assembly-plugin jar-with-dependencies make-assembly package single
get和post请求:
package com.dtwave.duruo;import org.apache.commons.io.IOUtils;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import java.io.IOException;import java.io.InputStream;public class HttpClientDemo { public static void main(String[] args) { System.out.println(doGetMethod("http://www.baidu.com")); System.out.println(doPostMethod("http://www.baidu.com")); } // get请求 public static String doGetMethod(String url){ // 获取httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建GET请求 // String url = "http://www.baidu.com"; HttpGet hg = new HttpGet(url); // 设置参数 hg.addHeader("Content-Type", "text/html;charset=utf-8"); String result = null; try { CloseableHttpResponse response = httpclient.execute(hg); InputStream in = response.getEntity().getContent(); result = IOUtils.toString(in); // 关闭流 in.close(); response.close(); } catch (Exception e) { e.printStackTrace(); } return result ; } // post请求方法 public static String doPostMethod(String url){ String res = null; // 获取httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建POST请求 // String url = "http://www.baidu.com"; HttpPost hp = new HttpPost(url); hp.addHeader("Content-Type", "text/html;charset=utf-8"); // try { CloseableHttpResponse chr = httpclient.execute(hp); InputStream in = chr.getEntity().getContent(); res = IOUtils.toString(in); } catch (Exception e) { e.printStackTrace(); } return res; }}
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年04月06日 06时50分20秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Java中有几种基本数据类型?它们分别占多大字节?
2021-05-15
Java中基本类型的转换规则
2021-05-15
如何将字符串写入文件?
2021-05-15
ie8、ie9获取input文件大小
2021-05-15
Mapper 接口方法如何与注解里的 SQL 进行绑定的?
2021-05-15
python安装和配置(win10)
2021-05-15
重构函数(1)条件合并
2021-05-15
2020编码大赛(1)题目
2021-05-15
BitChanger语言
2021-05-15
Pythagorea(3)第16-21章
2021-05-15
纪念碑谷(1-5章)
2021-05-15
基数树(radix tree)
2021-05-15
58Q游戏(4)73(5)85(6)98(7)
2021-05-15
独立钻石棋详解
2021-05-15
106 多米诺骨牌(12)119(8)130(9)142(10)150(11)
2021-05-15
算两次计数法
2021-05-15
点亮细胞171-180
2021-05-15
C++ Primer Plus读书笔记:c++字符串
2021-05-15
CSU 1757: 火车入站(区间覆盖的最大覆盖深度)
2021-05-15
C++ Primer Plus读书笔记:循环读取(错误处理)
2021-05-15