开发者

Capturing cookie responses in C#

开发者 https://www.devze.com 2023-01-10 11:27 出处:网络
Using Web Browser control or http request in C#, when a webs开发者_JAVA技巧ite creates a cookie (or tries to), is there a way to capture and display that cookie?Yes, cookies are returned in the Set-Co

Using Web Browser control or http request in C#, when a webs开发者_JAVA技巧ite creates a cookie (or tries to), is there a way to capture and display that cookie?


Yes, cookies are returned in the Set-Cookie header. You can use the HttpWebResponse.Headers collection to look through all of the headers and read out the cookies, or there's also the Cookies collection which wraps it for you.


Sure, you can use a tool like Fiddler or FireBug to capture and inspect the requests and responses sent during your session. I'm more familiar with Fiddler, so I'm basing my answer and example on it.

For example, if I log in to my company's Outlook Web Access portal, I enter my user name and password, and the OWA client writes a session cookie to my machine. If I have Fiddler running while I log in, I can see this cookie being written as part of the response from the server due to a successful log in:

**Cookies / Login**
  Set-Cookie: sessionid=d8ff0256-7339-4049-81c2-fae98f7c3ed5:0x409; path=/

If there are more cookies being sent down, you'll see them listed under the Cookies group.

I can see these cookies if I click on the resource that was requested (the page that I'm taken to after successful login), and then click on the Headers tab in the Response section of Fiddler.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消