日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpURLConnection 中Cookie 使用

發布時間:2024/9/30 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpURLConnection 中Cookie 使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如果想通過 HttpURLConnection 訪問網站,網站返回cookie信息,下次再通過HttpURLConnection訪問時,把網站返回 cookie信息再返回給該網站。可以使用下面代碼。

CookieManager manager = new CookieManager(); CookieHandler.setDefault(manager);

通過這兩行代碼就可以把網站返回的cookie信息存儲起來,下次訪問網站的時候,自動幫你把cookie信息帶上。

CookieManager還可以設置CookiePolicy。設置如下:

CookieManager manager = new CookieManager(); //設置cookie策略,只接受與你對話服務器的cookie,而不接收Internet上其它服務器發送的cookie manager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);

有關CookiePolicy請詳看: CookiePolicy 原理解析

CookieHandler 源碼分析

public abstract class CookieHandler {private static CookieHandler cookieHandler;public synchronized static CookieHandler getDefault() {SecurityManager sm = System.getSecurityManager();if (sm != null) {sm.checkPermission(SecurityConstants.GET_COOKIEHANDLER_PERMISSION);}return cookieHandler;}public synchronized static void setDefault(CookieHandler cHandler) {SecurityManager sm = System.getSecurityManager();if (sm != null) {sm.checkPermission(SecurityConstants.SET_COOKIEHANDLER_PERMISSION);}cookieHandler = cHandler;}public abstract Map<String, List<String>>get(URI uri, Map<String, List<String>> requestHeaders)throws IOException;public abstract voidput(URI uri, Map<String, List<String>> responseHeaders)throws IOException; }

CookieHandler是抽象類,內部提供了靜態的setDefault方法。
并且 private static CookieHandler cookieHandler; 是靜態的。
子類需要實現get()和put()方法。
get()方法返回該uri相關的cookie。
put()方法是存儲該uri相關的cookie。

jdk1.6中提供了CookieHandler的實現類CookieManager。

CookieManager 源碼分析

get()方法

CookieManager.get() 方法實現了從CookieStore中獲取該uri對應的cookie。

put() 方法


首先解析http 相應頭信息中的cookie,并存儲到 List cookies 中。


循環cookies中的cookie,根據設置的CookiePolicy來判斷是否接收該Cookie信息,
如果接收則存儲到CookieStore。

Cookie實現機制

這樣每次在調用HttpURLConnection訪問網站的時候,通過CookieHandler.getDefault()方法獲取CookieManager實例(靜態的方法,全局都可用)。
從解析http的響應頭中的cookie調用CookieHandler中的put方法存放到CookieStore中。
再次訪問網站的時候調用CookieHandler中的get方法獲取該uri響應的cookie,并提交到該站點中。
這樣開發人員就不需要干預cookie信息,則每次訪問網站會自動攜帶cookie。

代碼示例

本例子中使用到了CookieHandler、CookieManager 、CookieStore、 HttpCookie。

public class CookieManagerDemo {//打印cookie信息public static void printCookie(CookieStore cookieStore){List<HttpCookie> listCookie = cookieStore.getCookies();listCookie.forEach(httpCookie -> {System.out.println("--------------------------------------");System.out.println("class : "+httpCookie.getClass());System.out.println("comment : "+httpCookie.getComment());System.out.println("commentURL : "+httpCookie.getCommentURL());System.out.println("discard : "+httpCookie.getDiscard());System.out.println("domain : "+httpCookie.getDomain());System.out.println("maxAge : "+httpCookie.getMaxAge());System.out.println("name : "+httpCookie.getName());System.out.println("path : "+httpCookie.getPath());System.out.println("portlist : "+httpCookie.getPortlist());System.out.println("secure : "+httpCookie.getSecure());System.out.println("value : "+httpCookie.getValue());System.out.println("version : "+httpCookie.getVersion());System.out.println("httpCookie : "+httpCookie);});}public static void requestURL() throws Exception{URL url = new URL("http://192.168.3.249:9000/webDemo/index.jsp");HttpURLConnection conn = (HttpURLConnection)url.openConnection();String basic = Base64.getEncoder().encodeToString("infcn:123456".getBytes());conn.setRequestProperty("Proxy-authorization", "Basic " + basic);BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line = null;while((line=br.readLine())!=null){System.out.println(line);}br.close();}public static void main(String[] args) throws Exception {CookieManager manager = new CookieManager();//設置cookie策略,只接受與你對話服務器的cookie,而不接收Internet上其它服務器發送的cookiemanager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);CookieHandler.setDefault(manager);printCookie(manager.getCookieStore());//第一次請求requestURL();printCookie(manager.getCookieStore());//第二次請求requestURL();}}

從抓包結果中發現,第二次訪問該站點的時候,會自動攜帶Cookie信息。

本人簡書blog地址:http://www.jianshu.com/u/1f0067e24ff8????
點擊這里快速進入簡書

GIT地址:http://git.oschina.net/brucekankan/
點擊這里快速進入GIT

總結

以上是生活随笔為你收集整理的HttpURLConnection 中Cookie 使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。