public class MyPolicy : ICertificatePolicy
{
    public bool CheckValidationResult(ServicePoint srvPoint,
    X509Certificate cert, WebRequest request,
    int certificateProblem)
    {
return true;
    }
    public bool AcceptAllCertificatePolicy()
    {
return true;
    }
} private void main()
{
    ServicePointManager.CertificatePolicy = new MyPolicy();
    string url = "https://www.aaa.com/login.php?language=traditional";
    WebClient myWebClient = new WebClient();
    WebHeaderCollection myWebHeaderCollection = myWebClient.Headers;
    myWebClient.DownloadData(url);
    string Cookies = myWebClient.ResponseHeaders.Get("Set-Cookie");
    string postData = "username=" + textBox2.Text + "&password=" + textBox3.Text + "&login=确定&language=traditional";
    byte[] byteArray = Encoding.Default.GetBytes(postData);    myWebClient.Headers.Add("Referer", "https://www.aaa.com/login.php?language=traditional");
    myWebClient.Headers.Add("Cookie", Cookies);
    myWebClient.Headers.Add("Accept-Language", "zh-cn");
    myWebClient.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
    myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
    myWebHeaderCollection = myWebClient.Headers;    byte[] responseArray = myWebClient.UploadData("https://www.aaa.com/login.php", "POST", byteArray);//这步出现cookie过期提示
    string SourceCode = Encoding.Default.GetString(responseArray);
    textBox1.Text = SourceCode;
}
使用cookie时提示过期

解决方案 »

  1.   

    我测试过了,你的代码好象没有问题啊,但你的https://www.aaa.com/login.php?language=traditional是没有的,找不到这个页面啊,我用了一个登陆测试了一下,完全可以,甚至你的代码写的很多余,你应该先确定string Cookies = myWebClient.ResponseHeaders.Get("Set-Cookie");
    是否为null,可以这样啊:            
    for (int i = 0; i < myWebClient.ResponseHeaders.Keys.Count; i++)
      {
         txtOut.Text = txtOut.Text + i + " " + Regex.Replace(myWebClient.ResponseHeaders.Get(i), "<[^>]*>", "");
         txtOut.Text = txtOut.Text + "\n";
       }
    如果没有Set-Cookie 的话,你把 myWebClient.Headers.Add("Cookie", Cookies);就为null了
      

  2.   

    我用HttpWatch查看过的Cookies不是null的
      

  3.   

    提示是Sorry, your session has expired