Cookie 似乎没有在 HttpWebRequest 中使用



在我的问题无法使用httpwebrequests登录后,我成功登录了。我在页面上说"谢谢您登录" ,但是之后我似乎没有登录。

对我来说,这看起来像是一个饼干问题。在firebug中,饼干似乎是httponly,这可能是一个问题吗?如何使HttpWebRequest使用cookie?

这是我用来登录的代码:

string url = "http://www.warriorforum.com/login.php?do=login";
var bytes = Encoding.Default.GetBytes(@"vb_login_username=USERNAME&cookieuser=1&vb_login_password=&s=&securitytoken=guest&do=login&vb_login_md5password=d9350bad28eee253951d7c5211e50179&vb_login_md5password_utf=d9350bad28eee253951d7c5211e50179");
var container = new CookieContainer();
var request = (HttpWebRequest)(WebRequest.Create(url));
request.CookieContainer = container;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.KeepAlive = true;
request.AllowAutoRedirect = true;
request.AllowWriteStreamBuffering = true;
request.CookieContainer = container;
using (var requestStream = request.GetRequestStream())
    requestStream.Write(bytes, 0, bytes.Length);
var requestResponse = request.GetResponse();
using (var responsStream = requestResponse.GetResponseStream())
    if (responsStream != null)
    {
        using (var responseReader = new StreamReader(responsStream))
        {
            var responseStreamReader = responseReader.ReadToEnd();
            richTextBox1.Text = responseStreamReader; //this is to read the page source after the request
      }
    }
}

HttpWebRequest.CookieContainer的手册页说:

CookieContainer属性提供了包含与此请求相关的cookie的CookieContainer类的实例。

你做:

var container = new CookieContainer();

因此,在每个请求下,您都以新的CookieContainer开头,而无需任何cookie。使container成为班级成员,仅实例化一次。

相关内容

  • 没有找到相关文章

最新更新