模拟登录(一次自动调转)

来源:互联网 发布:软件项目管理 考试 编辑:程序博客网 时间:2024/05/29 04:00
 class Class3    {        public static void A()        {            HttpWebRequest request = null;            HttpWebResponse response = null;            string gethost = string.Empty;            CookieContainer cc = new CookieContainer();            string Cookiesstr = string.Empty;            try            {                //第一次POST请求                string postdata = @"username=%E5%B0%8F%E5%B0%8F%E7%9A%84%E5%85%94%E5%85%94&loginFlag=1&userId=2356208%0D%0A";//模拟请求数据                string LoginUrl = "http://szbbs.sznews.com/logging.php?action=login&curpage=%2Fsearch.php%3Fascdesc%3Ddesc%26before%3D%26formhash%3D81d3dcc0%26orderby%3Dlastpost%26searchsubmit%3Dtrue%26srchfid%255B%255D%3Dall%26srchfilter%3Dall%26srchfrom%3D0%26srchtxt%3Dtest%26srchtypeid%3D%26srchuname%3D";                request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类                request.Method = "POST";//数据提交方式为POST                //模拟头                request.ContentType = "application/x-www-form-urlencoded";                byte[] postdatabytes = Encoding.GetEncoding("gb2312").GetBytes(postdata);                request.ContentLength = postdatabytes.Length;                request.Referer = "login.sina.com.cn";                request.AllowAutoRedirect = false;                request.CookieContainer = cc;                request.KeepAlive = true;                //提交请求                Stream stream;                stream = request.GetRequestStream();                stream.Write(postdatabytes, 0, postdatabytes.Length);                stream.Close();                //接收响应                response = (HttpWebResponse)request.GetResponse();                //保存返回cookie                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);                CookieCollection cook = response.Cookies;                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);                Cookiesstr = strcrook;                //取第一次GET跳转地址                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));                string content = sr.ReadToEnd();                response.Close();            }            catch (Exception)            {                //第一次POST出错;            }            try            {                //Cookiesstr = @"cdb_sid=CdQgsf; cdb_cookietime=0; cdb_auth=FoJk27b3Ynm3s%2FvMEfuXmCtyweSWb9Go7lE7emrVJ80KJ%2FWpT1SfdcHvXPSiGsnM%2BVMstkFU6ng";                gethost = "http://szbbs.sznews.com/search.php"; //第一次GET跳转地址                request = (HttpWebRequest)WebRequest.Create(gethost);                request.Method = "GET";                request.KeepAlive = true;                request.Headers.Add("Cookie:" + Cookiesstr);                request.CookieContainer = cc;                request.AllowAutoRedirect = false;                response = (HttpWebResponse)request.GetResponse();                //设置cookie                   Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);                //取再次跳转链接                   StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);                string content = sr.ReadToEnd();                request.Abort();                sr.Close();                response.Close();            }            catch (Exception)            {                //第一次GET出错               }                   }    }

原创粉丝点击