
HttpClient先post登陆,在get数据
发布日期:2021-05-06 17:28:43
浏览次数:15
分类:技术文章
本文共 2943 字,大约阅读时间需要 9 分钟。
我用的是httpclient-4.5.1.jar,如果httpclient版本太低的话,此方法可能用不了,需要用手工设置cookie.那种方法,暂不讨论,主要思路就是通过登陆时获取返回的cookie,再讲coolie的值填到新的请求头中,不过这种方法一般不怎么遇到,基本上对面接口都会直接返回给你一个token,你下次访问直接将这个token作为参数传过去就能获取自己想要的数据.
package test;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.apache.http.HttpEntity;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.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import com.alibaba.fastjson.JSONArray;public class JKTest03 { public static void main(String[] args) throws Exception { long st=new Date().getTime(); CloseableHttpClient http = HttpClients.createDefault(); try { String url = "post"; HashMapmap = new HashMap (); map.put("username", "admin"); map.put("password", "pass"); getlogin(http, map, url);//登陆验证 String url2 = "get"; String list = Get(http, url2);//获取数据 System.out.println(list); } finally { http.close(); } long et=new Date().getTime(); System.out.println("共费时:"+(et-st)); // System.out.println("结束"); } public static String getlogin(CloseableHttpClient client, Map map, String url) throws Exception { String body = ""; String encoding = "utf-8"; // 创建post方式请求对象 HttpPost httpPost = new HttpPost(url); // 装填参数,常规格式 /* * List nvps = new ArrayList (); if (map != null) { * for (Entry entry : map.entrySet()) { * //System.out.println(entry.getKey()+"=="+ entry.getValue()); nvps.add(new * BasicNameValuePair(entry.getKey(), entry.getValue())); } } // 设置参数到请求对象中 * httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); * // 设置参数到请求对象中 httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding)); */ //参数是json格式 httpPost.setEntity(new StringEntity(JSONArray.toJSONString(map))); // 设置header信息 // 指定报文头【Content-type】、【User-Agent】 //参数是json的需要将Content-type设为application/json httpPost.setHeader("Content-type", "application/json");//默认是:application/x-www-form-urlencoded httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); // 执行请求操作,并拿到结果(同步阻塞) CloseableHttpResponse response = client.execute(httpPost); // response. // 获取结果实体 HttpEntity entity = response.getEntity(); if (entity != null) { // 按指定编码转换结果实体为String类型 body = EntityUtils.toString(entity, encoding); } // 释放链接 response.close(); return body; } public static String Get(CloseableHttpClient http, String url) throws Exception { HttpGet httpget = new HttpGet(url); String body = ""; CloseableHttpResponse response = http.execute(httpget); // response. // 获取结果实体 HttpEntity entity = response.getEntity(); if (entity != null) { // 按指定编码转换结果实体为String类型 body = EntityUtils.toString(entity, "utf-8"); } // 释放链接 response.close(); return body; }}
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月07日 18时55分51秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
1093 Count PAT‘s (25分) 含DP做法
2019-03-05
一篇解决JMM与volatile详解(二)
2019-03-05
数据结构之数组与经典面试题(二)
2019-03-05
无锁并发框架-Disruptor的使用(二)
2019-03-05
Android wm命令
2019-03-05
Android4.4 平板背光设置
2019-03-05
codeforces The Eternal Immortality 题解
2019-03-05
微信js-sdk使用简述(分享,扫码功能等)
2019-03-05
selenium 的介绍和爬取 jd数据
2019-03-05
mxsrvs支持thinkphp3.2伪静态
2019-03-05
mui HTML5 plus 下载文件
2019-03-05
DSP开发板准备
2019-03-05
c++中ifstream及ofstream超详细说明
2019-03-05
c++中explicit和mutable关键字探究
2019-03-05
c语言结构体字节对齐详解
2019-03-05
Deep residual learning for image recognition
2019-03-05
Python 知识点总结篇(3)
2019-03-05
爬取网易科技滚动新闻
2019-03-05
vuex modules
2019-03-05
sleep、wait、yield、join——简介
2019-03-05