最近在使用接口时候,我使用get请求时,需要携带登录态,所以在get请求的时候我需要在head里面把cookie给加上,添加方式get和post完全不一样

Post方式添加cookie

           httpPost = new HttpPost(url);
//添加代理配置 // 设置代理
RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
System.out.println("请求的参数为:"+elem.getKey()+":"+elem.getValue());
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"utf-8");
httpPost.setEntity(entity);
} //设置头部信息
httpPost.setHeader("Referer","http://oms.hqygou.com/order/temp/new");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpPost.setHeader("X-Requested-With","XMLHttpRequest");
httpPost.setHeader("Cookies",cookies);

  get请求设置头部信息

	                HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Connection","keep-alive");
httpGet.addHeader(new BasicHeader("Cookie", cookies));

  注意了使用:setHeader如果有这个cookie就会在后面直接添加这个cookie

addHeader没有这个cookie就新增这个cookie

httpclient使用head添加cookie的更多相关文章

  1. Android 添加cookie

    /** * 添加cookie * * @param url */ private void setCookie(String url) { // 获取uid String uid = UserData ...

  2. .net 添加Cookie的4种方法

    第一种添加Cookie方法 HttpCookie myCookie = new HttpCookie("userrole"); myCookie.Values["a&qu ...

  3. Android 应用内HttpClient 与 WebView 共享 Cookie

    DefaultHttpClient httpclient=....; String toUrl="https://cap.cityu.edu.hk/studentlan/details.as ...

  4. response.addCookie(cookie)添加cookie失败.

    两个if循环能进来,创建的两个cookie也能通过控制台输出.  但是却添加失败. 原因是:request.getRequestDispatcher("/MainFrame").f ...

  5. selenium3.7+ python3 添加cookie模拟登陆

    一.背景介绍 最近做一个爬虫项目,用selenium调用浏览器去获取渲染后的源码,但是每次登陆都需要手机验证,这真的是头痛啊,这种验证方式不要想着去破解,还是老老实实用手机收验证码去吧!反正我是不知道 ...

  6. selenium webdriver 如何添加cookie

    一. webdriver中常用的cookie方法 webdriver中提供了操作cookie的相关方法: get_cookies()                  获得cookie信息 add_c ...

  7. postman(十一):添加cookie

    有些接口在调用时,需要提供权限,如下 这个时候可以通过添加cookie的方式跳过验证 为了更方便地获取cookie等信息,可以在chrome中安装一个插件:Postman Interceptor,配合 ...

  8. js如何在浏览器添加cookie(添加、获取、删除)

    首先,简单用js将cookie保存到浏览器中,具体可按F12在这里查看(火狐浏览器) 1.js脚本方法 1)添加cookie方法 //添加cookie var addCookie = function ...

  9. katalon系列十五:给浏览器添加cookie自动登陆

    import org.openqa.selenium.Cookieimport org.openqa.selenium.WebDriverimport com.kms.katalon.core.web ...

随机推荐

  1. my08_mysqldump+binlog server备份

    备份策略描述 ******************************************* mysqldump备份适用于小数据量的备份,比如100G以下的数据量,就可以使用逻辑备份 举例两个 ...

  2. vue-cli中webpack配置解析

    版本号 vue-cli 2.8.1 (终端通过vue -V 可查看) vue 2.2.2 webpack 2.2.1 目录结构 ├── README.md ├── build │ ├── build. ...

  3. 使用EventBus实现兄弟组件之间的通信

    使用EventBus实现兄弟组件之间的通信 需求:为了实现菜单折叠的效果,例如http://blog.gdfengshuo.com/example/work/#/dashboard header组件和 ...

  4. 剑指offer中经典的算法题之从头到尾打印链表

    话不多说上代码: 我自己的算法是: /** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int ...

  5. 《我在谷歌大脑见习机器学习的一年:Node.js创始人的尝试笔记》阅读笔记

    文章来源:https://www.toutiao.com/i6539751003690893828/?tt_from=weixin_moments&utm_campaign=client_sh ...

  6. 3d Max 2015安装失败怎样卸载3dsmax?错误提示某些产品无法安装

    AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...

  7. PlayMaker Destroy Self 和 Destroy Object 和 Set Visibility

    1. 这个销毁是销毁状态机所在的游戏物体,不能销毁父物体. 2. 这个销毁只要把想销毁的游戏物体拖进去就可以. 3. 这个其实不是真正的销毁游戏对象,只是把它的 MeshRenderer 组件关上了, ...

  8. GridLayout(网格布局)

    常用属性: 排列对齐: ①设置组件的排列方式:  android:orientation=""     vertical(竖直,默认)或者horizontal(水平) ②设置组件的 ...

  9. Spring中的一些常用接口

    一.ApplicationContextAware接口 注:可以在spring容器初始化的时候调用setApplicationContext方法,从而获得ApplicationContext中的所有b ...

  10. Linux禁ping

    A.临时允许PING操作的命令为:# >/proc/sys/net/ipv4/icmp_echo_ignore_all B.永久允许PING配置方法. /etc/sysctl.conf 中增加一 ...