httpclient使用head添加cookie

最近在使用接口时候,我使用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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值