c#模擬登錄獲取Cookie


這里是使用 CookieContainer 獲取 到 Cookie

string contentUrl = "https://home.cnblogs.com/u/xinjian/";
            HttpWebRequest reqContent = (HttpWebRequest)WebRequest.Create(contentUrl);
            reqContent.Method = "GET";
            reqContent.AllowAutoRedirect = false;//服務端重定向。一般設置false
            reqContent.ContentType = "application/x-www-form-urlencoded";//數據一般設置這個值,除非是文件上傳
    var  cookie = new CookieContainer();
reqContent.CookieContainer = cookie; 
reqContent.CookieContainer.SetCookies(reqContent.RequestUri, cookies);
//將登錄的cookie值賦予此次的請求。
HttpWebResponse respContent
= (HttpWebResponse)reqContent.GetResponse();
string html = new StreamReader(respContent.GetResponseStream()).ReadToEnd();
//獲取到的cookie就在變量cookie中。

 

https://www.cnblogs.com/xinjian/p/6343367.html


免責聲明!

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



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