Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

c# - Why can I not iterate through the cookies when I perform a request to an url?

I am trying to perform a get request to https://www.footlocker.dk/. I need to obtain the session that is created, when visiting this url.

I perform the request as following:

            string url = "https://www.footlocker.dk/";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Proxy = null;
            request.CookieContainer = new CookieContainer();

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {

                foreach (Cookie cookie in response.Cookies)
                {
                    Console.WriteLine("name=" + cookie.Name);
                    Console.WriteLine("value=" + cookie.Value);
                    Console.WriteLine("path=" + cookie.Path);
                    Console.WriteLine("domain=" + cookie.Discard);
                    
                }
            }

However it doesnt return me any cookies..

I need these cookies, when I want to go ahead and make a post request afterward, to perform a specific action on this url.

Regards!

question from:https://stackoverflow.com/questions/65887230/why-can-i-not-iterate-through-the-cookies-when-i-perform-a-request-to-an-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check the response payloads:

You'll see that the call: https://www.footlocker.dk/api/session?timestamp=1611587725706 has a response header like this:

set-cookie: JSESSIONID=orfy8ma22lox1nd8wxdyf8h4e.fzcxwefapipdb018883; Path=/; HTTPOnly

enter image description here

The https://www.footlocker.dk/ sets no such cookies. Bear in mind that if you go throug the calls, you'll see multiple cookie sets, and you can't be absolutely sure which one you need to make things work, though I believe that you can get the cookie you need just by making the session call.

The final cookie definition looks like this:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...