.net 模拟登陆 post https 请求跳转页面

AllowAutoRedirect property is true, the Referer property is set automatically when the request is redirected to another site.">如果 AllowAutoRedirect 属性为 true,则 Referer 属性在请求被重定向到另一个站点时自动设置。

Referer HTTP header, set the Referer property to null.">若要清除 RefererHTTP 标头,请将 Referer 属性设置为 null
如果设置了  Referer 则 指定到某个站点. 被这东西小坑了一下 涨姿势。

 

 

          private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
          {
               return true; //总是接受安全证书
          }

          private void test() 
          {
               ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
               CookieCollection _cookies = null; //后续使用
               var addRess = "https://xxxxxxxxxx";
               var data = "account=xxxxxxx&password=xxxxx";

               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(addRess);
               request.CookieContainer = new CookieContainer();
               request.Timeout = 1000 * 60;
               request.Method = "POST";               
               request.ContentType = "application/x-www-form-urlencoded";
               request.AllowAutoRedirect = true;
               request.Referer = addRess;

               byte[] bs = Encoding.UTF8.GetBytes(data);
               HttpWebResponse response = null;
               StreamReader myStreamReader = null;
               Stream myRequestStream = null;
               request.ContentLength = bs.Length;
               string retString = string.Empty;
               try
               {
                    using (myRequestStream = request.GetRequestStream())
                    {
                         myRequestStream.Write(bs, 0, bs.Length);
                         myRequestStream.Close();
                    }
                    response = (HttpWebResponse)request.GetResponse();
                    _cookies = response.Cookies;
                    using (Stream myResponseStream = response.GetResponseStream())
                    {
                         using (myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("BIG5")))
                         {
                              retString = myStreamReader.ReadToEnd();
                         }
                    }
               }
               catch (Exception)
               {

                    throw;
               }
               finally
               {
                    if (myRequestStream != null)
                    {
                         myRequestStream.Close();
                    }
                    if (response != null)
                    {
                         response.Close();
                    }
                    if (myStreamReader != null)
                    {
                         myStreamReader.Close();
                    }
               }
          }

 

posted on 2015-04-30 14:48  无觉-李敏  阅读(2466)  评论(0编辑  收藏  举报