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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM