C# 如何解析 HttpWebResponse.Headers.Keys 以获取返回的 Set-Cookie 会话 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1055853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse HttpWebResponse.Headers.Keys for a Set-Cookie session id returned
提问by Maxim Zaslavsky
I'm trying to create an HttpWebRequest/HttpWebResponse session with an ASP.NET website to later parse an HTML form through url params (this part I know how to do), but I do not understand how to parse and set a cookie such as the session id. In Fiddler, it shows that the ASP.NET Session ID is returned through Set-Cookie in the response to the request to the / path of the url, but how can I extract this session id and set it as a cookie for the next HttpWebRequest? I understand that this Set-Cookie header would be found in HttpWebResponse.Headers.Keys, but is there a direct path to parsing it? Thanks!
我正在尝试使用 ASP.NET 网站创建一个 HttpWebRequest/HttpWebResponse 会话,以便稍后通过 url 参数解析 HTML 表单(这部分我知道该怎么做),但我不明白如何解析和设置 cookie,例如会话标识。在Fiddler中,它显示ASP.NET Session ID是通过Set-Cookie在对url的/路径的请求的响应中返回的,但是如何提取此会话ID并将其设置为下一个HttpWebRequest的cookie ? 我知道这个 Set-Cookie 标头可以在 HttpWebResponse.Headers.Keys 中找到,但是有解析它的直接路径吗?谢谢!
采纳答案by Dan Herbert
The .NET framework will manage cookies for you. You don't have to concern yourself with parsing the cookie information out of the headers or adding a cookie header to your requests.
.NET 框架将为您管理 cookie。您不必担心从标头中解析 cookie 信息或将 cookie 标头添加到您的请求中。
To store and send your session ID, use the Cookie
and CookieContainer
classes to store them and then make sure you send your cookies with every request.
要存储和发送您的会话 ID,请使用Cookie
和CookieContainer
类来存储它们,然后确保随每个请求发送您的 cookie。
The following example shows how to do this. The CookieContainer, 'cookieJar
' can be shared across multiple domains and requests. Once you add it to a request object, the reference to it will also be added to the response object when the response is returned.
以下示例显示了如何执行此操作。CookieContainer“ cookieJar
”可以跨多个域和请求共享。将它添加到请求对象后,在返回响应时,对它的引用也将添加到响应对象中。
CookieContainer cookieJar = new CookieContainer();
var request = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
request.CookieContainer = cookieJar;
var response = request.GetResponse();
foreach (Cookie c in cookieJar.GetCookies(request.RequestUri))
{
Console.WriteLine("Cookie['" + c.Name + "']: " + c.Value);
}
The output of this code will be:
此代码的输出将是:
Cookie['PREF']: ID=59e9a22a8cac2435:TM=1246226400:LM=1246226400:S=tvWTnbBhK4N7Tlpu
Cookie['PREF']: ID=59e9a22a8cac2435:TM=1246226400:LM=1246226400:S=tvWTnbBhK4N7Tlpu
回答by user238397
The answer from Dan Herbert helped me really. I appreciate your help.
Dan Herbert 的回答对我很有帮助。我感谢您的帮助。
Just want to post my usage - hope it helps some one at some point of time. My requirement is that I need to send back cookies from first http post response to second http post request.
只是想发布我的用法 - 希望它在某个时间点对某些人有所帮助。我的要求是我需要将 cookie 从第一个 http post 响应发送回第二个 http post 请求。
1st:
第一:
CookieContainer cookieJar = new CookieContainer();
request.CookieContainer = cookieJar;
....
CookieCollection setCookies = cookieJar.GetCookies(request.RequestUri);
2nd:
第二:
CookieContainer cc = new CookieContainer();
cc.Add(setCookies);
request.CookieContainer = cc;
回答by Kriss
hum I may be wrong but from what I am observing lately
嗯,我可能错了,但从我最近的观察来看
Cookies from a first response, don't include the 'set cookie' as cookies that come in the header (for example some session id...) in the case of a 302 (redirect) status
来自第一个响应的 cookie,在 302(重定向)状态的情况下,不要将“设置 cookie”作为 cookie 包含在标头中(例如某些会话 ID...)
If the autofollowredirect is set to true, then the set cookie are processed, and the subsequent request which is done automatically, will include those cookies defined by set cookie on the first call
如果 autofollowredirect 设置为 true,则处理设置的 cookie,并且自动完成的后续请求将包括第一次调用时由 set cookie 定义的那些 cookie
If autofollowredirect is set to false then the first request doesn't get the cookies positionned by the set cookie, and I guess and this is also my queston if anyone know, that the only way to subsequently have those cookies in next request, is parse the set cookies ?
如果 autofollowredirect 设置为 false,那么第一个请求不会获得由设置的 cookie 定位的 cookie,我想这也是我的问题,如果有人知道,随后在下一个请求中使用这些 cookie 的唯一方法是解析设置的饼干?
回答by Elia
I have the same problem (with amazon) I use the following regexp:
我有同样的问题(与亚马逊)我使用以下正则表达式:
string regexp = "(?<name>[^=]+)=(?<val>[^;]+)[^,]+,?";);
MatchCollection myMatchCollection = Regex.Matches(cookiesStr, regexp);
foreach (Match myMatch in myMatchCollection)
{
string cookieName = myMatch.Groups["name"].ToString();
string cookieVal = myMatch.Groups["val"].ToString();
Cookie cookie = new Cookie(cookieName, cookieVal);
cookies.Add(cookie);
}
Note that I only care about the cookie name/value...
请注意,我只关心 cookie 名称/值...
good luck Elia
祝你好运伊利亚