HttpWebResponse.Cookies空,尽管Set-Cookie头(无重定向) [英] HttpWebResponse.Cookies empty despite Set-Cookie Header (no-redirect)

查看:460
本文介绍了HttpWebResponse.Cookies空,尽管Set-Cookie头(无重定向)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力弄清楚这里出了什么问题。我发送登录信息,我可以看到标题中的Set-Cookie的正确值,但是Cookies集合没有被填充。

I'm struggling to figure out what is wrong here. I'm sending login information, I can see the Set-Cookie in the Header with the correct value, but the Cookies collection is not getting filled.

这是HTTPS,登录自动重定向,但是我通过AllowAutoRedirect = false禁用它来尝试解决此问题。

This is HTTPS, the login auto-redirects, but I disabled it with AllowAutoRedirect=false to try to troubleshoot this issue.

在此屏幕截图中,您可以轻松查看调试信息, cookie应该设置。我将httpWebRequest.Cookie设置为新的CookieCollection。

In this screenshot, you can easily see the debug information and that the cookie should be getting set. I am setting my httpWebRequest.Cookie to a new CookieCollection.

HttpWebRequest httpRequest;
CookieContainer reqCookies = new CookieContainer();
string url = "https://example.com";
string[] email = user.Split('@');
email[0] = System.Web.HttpUtility.UrlEncode(email[0]);
user = email[0] + "@" + email[1];
pass = System.Web.HttpUtility.UrlEncode(pass);

string postData = "email=" + user + "&password=" + pass;
byte[] byteData = Encoding.UTF8.GetBytes(postData);

httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
httpRequest.Referer = url;
httpRequest.CookieContainer = reqCookies;
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1003.1 Safari/535.19";
httpRequest.Accept = "text/html, application/xhtml+xml, */*";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = byteData.Length;
using (Stream postStream = httpRequest.GetRequestStream())
{
    postStream.Write(byteData, 0, byteData.Length);
    postStream.Close();
}

httpRequest.AllowAutoRedirect = false;
HttpWebResponse b = (HttpWebResponse)httpRequest.GetResponse();

尝试了连接到 http://www.yahoo.com ,并将Cookie放入我的收藏... Argh ...

Tried the exact same code connecting to http://www.yahoo.com and the cookies are put into my collection... Argh...

以下是Set-Cookie Header值:

Here is the Set-Cookie Header value:


s = 541E2101-B768-45C8-B814-34A00525E50F; Domain = example.com; Path = /;
Version = 1

s=541E2101-B768-45C8-B814-34A00525E50F; Domain=example.com; Path=/; Version=1


推荐答案

阅读C#中的Cookie是由C#ASP.NET应用程序创建的...;)

I've found that issue as well, when reading Cookies in C# that were created by a C# ASP.NET app... ;)

不知道它是否与它有关,但我发现两个在我的case设置的cookie写在一个单一的Set-Cookie头,cookie有效负载用逗号分隔。所以我调整了AppDeveloper的解决方案来处理这个多cookie的问题,以及修复我在评论中提到的名称/值的东西。

Not sure if it has to do with it, but I found that the two Cookies that are set in my case are written in a single Set-Cookie header, with the cookie payload separated by commas. So I adapted AppDeveloper's solution to deal with this multiple-cookie issue, as well as fixing the name/value thing I mentioned in the comments.

private static void fixCookies(HttpWebRequest request, HttpWebResponse response) 
{
    for (int i = 0; i < response.Headers.Count; i++)
    {
        string name = response.Headers.GetKey(i);
        if (name != "Set-Cookie")
            continue;
        string value = response.Headers.Get(i);
        foreach (var singleCookie in value.Split(','))
        {
            Match match = Regex.Match(singleCookie, "(.+?)=(.+?);");
            if (match.Captures.Count == 0)
                continue;
            response.Cookies.Add(
                new Cookie(
                    match.Groups[1].ToString(), 
                    match.Groups[2].ToString(), 
                    "/", 
                    request.Host.Split(':')[0]));
        }
    }
}

这篇关于HttpWebResponse.Cookies空,尽管Set-Cookie头(无重定向)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆