感谢所有回复。这是我奇怪的解决方案。:) 我写这个是因为将来有人可能需要它。
我收到的 cookie 不包含特定的 [name,value] 对,例如 [logged,true]。我收到的唯一东西是:
Set-Cookie: ASP.NET_SessionId=ah0b2kj40oi0vuufv0mmot35; path=/; HttpOnly\r\n
所以,我以为我走错了方向,并试图找到另一种方法来分析登录是否成功。我的解决方案是使用响应的 StatusCode。我意识到(感谢 Jason 关于错误代码 401 的评论)如果登录成功,服务器会返回一个 HTTP 302 Found 状态代码。但如果登录不成功,它会返回相同的登录页面(即 HTTP 200 OK)。因此,根据收到的响应的 HTTP 代码,我决定它是否成功。这是示例代码:
//In LoginForm.cs
if (((HttpWebResponse)request.GetResponse()).StatusCode.ToString().Equals("Found"))
{
nextUrl = ((HttpWebResponse)request.GetResponse()).Headers.Get(4);
StringBuilder FullUrl = new StringBuilder(this.server_address);
FullUrl.Append(nextUrl);
this.setSecretURL(FullUrl.ToString());
setLoginSuccess(true);
// now we can send out cookie along with a request for the protected page
request = WebRequest.Create(SECRET_PAGE_URL) as HttpWebRequest;
request.CookieContainer = cookies;
StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
// and read the response
result = responseReader.ReadToEnd();
responseReader.Close();
} else
{
setLoginSuccess(false);
}