Get cookies from httpwebrequest

后端 未结 3 1128
-上瘾入骨i
-上瘾入骨i 2020-12-16 18:33

I\'m trying to get all cookies from a website using this code

        CookieContainer cookieJar = new CookieContainer();

        var request = (HttpWebReque         


        
3条回答
  •  离开以前
    2020-12-16 19:16

    To get the list of cookies, you can use the below method;

    private async Task> GetCookies(string url)
    {
        var cookieContainer = new CookieContainer();
        var uri = new Uri(url);
        using (var httpClientHandler = new HttpClientHandler
        {
            CookieContainer = cookieContainer
        })
        {
            using (var httpClient = new HttpClient(httpClientHandler))
            {
                await httpClient.GetAsync(uri);
                return cookieContainer.GetCookies(uri).Cast().ToList();
            }
        }
    }
    

提交回复
热议问题