httpclient向浏览器发送get和post请求
get请求代码实现
public static void main(String[] args) {
CloseableHttpClient httpClient = null; //请求对象
CloseableHttpResponse response = null; //返回对象
HttpEntity entity = null; //返回主体
String responseContent = null; //将返回的主题转换成字符串
String url = "http://127.0.0.1:5000";//请求的URL
try {
httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "utf-8");
System.out.println(httpGet.getURI());
System.out.println(responseContent);
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
post请求代码实现
public static void main(String[] args) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
String url = "http://127.0.0.1:5000/login";
try {
httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "bokeyuan"));
nameValuePairs.add(new BasicNameValuePair("password", "123456"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
response = httpClient.execute(httpPost);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "utf-8");
System.out.println(responseContent);
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
httpclient向浏览器发送get和post请求的更多相关文章
- 20200726_java爬虫_使用HttpClient模拟浏览器发送请求
浏览器获取数据: 打开浏览器 ==> 输入网址 ==> 回车查询 ==> 返回结果 ==> 浏览器显示结果数据 HttpClient获取数据: 创建HttpClient ==& ...
- 解决火狐浏览器发送jquery的ajax请求无效的问题
今天遇到这样一个问题: 页面在chrome下发送ajax的请求是没有问题的,但是在firfox下无效. 代码大致如下: //前面省略 <form> ..... <button cla ...
- 使用HttpClient配置代理服务器模拟浏览器发送请求调用接口测试
在调用公司的某个接口时,直接通过浏览器配置代理服务器可以请求到如下数据: 请求url地址:http://wwwnei.xuebusi.com/rd-interface/getsales.jsp?cid ...
- HttpClient服务端发送http请求
本来以为对跨域问题的处理已经比较熟练了.可以通过jsonp.document.domain+iframe.window.name.window.postMessage.服务器上设置代理页面来解决.但还 ...
- 使用HttpClient来异步发送POST请求并解析GZIP回应
.NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...
- httpClient模拟浏览器发请求
一.介绍 httpClient是Apache公司的一个子项目, 用来提高高效的.最新的.功能丰富的支持http协议的客户端编程工具包.完成可以模拟浏览器发起请求行为. 二.简单使用例子 : 模拟浏览器 ...
- 关于对浏览器发送POST请求的一点研究
网上对与HTTP的Method,GET和POST的区别,说得毕竟详细.然后提到一点,说浏览器对两者的还有一个比较容易让人忽略的区别就是:POST会分2次发送,而GET只1次. GET发送1次,这个没什 ...
- HttpClient发送get,post接口请求
HttpClient发送get post接口请求/* * post * @param url POST地址 * @param data POST数据NameValuePair[] * @retur ...
- java使用HttpClient 发送get、pot请求
package eidolon.messageback.PostUtil; import java.io.BufferedReader; import java.io.IOException; imp ...
随机推荐
- 工控安全入门(五)—— plc逆向初探
之前我们学习了包括modbus.S7comm.DNP3等等工控领域的常用协议,从这篇开始,我们一步步开始,学习如何逆向真实的plc固件. 用到的固件为https://github.com/ameng9 ...
- OCP/OCA Oracle 学习001
select * from TEST3 t select object_type, count(object_type) from user_objects group by object_type ...
- PLSQLDeveloper链接报错 解决办法
PLSQL Developer 9.06.1665中文破解版 亲们,win7 64位系统现在还没有PLSQLDeveloper可以使用,但是怎么办呢.好的,下面教大家怎么在64位系统下安装PLSQLD ...
- hive作业的优化策略
Mapreduce自身的特点: 1.IO和网络负载大:优化策略:减少IO和网络负载. 2.内存负载不大.优化策略:增大内存使用率: 3.CPU负载不大.优化策略:增大CPU使用率: (hive的优化应 ...
- 转载:腾讯与新浪的通过IP地址获取当前地理位置(省份)的接口
腾讯的接口是 ,返回数组 http://fw.qq.com/ipaddress 返回值 var IPData = new Array("61.135.152.194"," ...
- https://vjudge.net/problem/2198220/origin
https://vjudge.net/problem/2198220/origin枚举等差数列第一个和第二个,然后二分确定数列后面是否存在,复杂度比较玄学,卡过了. #include<iostr ...
- [Array]414. Third Maximum Number
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- Thinkphp 架构笔记
多个模块的时候,公共模块Common必须和其他模块放在同一个目录下,否则拓展配置“LOAD_EXT_CONFIG”会无效
- [转]Event loop——浏览器和Node区别
最近对Event loop比较感兴趣,所以了解了一下.但是发现整个Event loop尽管有很多篇文章,但是没有一篇可以看完就对它所有内容都了解的文章.大部分的文章都只阐述了浏览器或者Node二者之一 ...
- 你不知道的javascript -- 数据类型
1. 数据类型 在js中有7中数据类型 其中6种是基本类型 包括 null, undefined, boolean, number, string和symbol,还有一种是引用类型object 但是判 ...