C# 保持登陸狀態並抓取資料

來源:互聯網
上載者:User
string url1 = @"http://login.vancl.com/Login/";                   //提交註冊資訊的網址
            string url2 = @"http://my.vancl.com/User/User_Order_List.aspx";    //需要下載的網頁

            string indata = "mailto:Logintrue=true&UserName=jsycywm@hotmail.com&PassWord=我的密碼&x=45&y=15";

            CookieContainer myCookieContainer = new CookieContainer();                      //建立一個CookieContainer來存放Cookie集合
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url1);      //建立一個HttpWebRequest
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.ContentLength = indata.Length;
            //myHttpWebRequest.ac
            myHttpWebRequest.Method = "POST";
            myHttpWebRequest.CookieContainer = myCookieContainer;                           //設定HttpWebRequest的CookieContainer為剛才建立的那個myCookieContainer
            Stream myRequestStream = myHttpWebRequest.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("GB2312"));
            myStreamWriter.Write(indata);           //把資料寫入HttpWebRequest的Request流

            myStreamWriter.Close();
            myRequestStream.Close();                //關閉開啟對象

            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();        //建立一個HttpWebResponse
            myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);      //擷取一個包含url的Cookie集合的CookieCollection
            Stream myResponseStream = myHttpWebResponse.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8 );
            string outdata = myStreamReader.ReadToEnd();//把資料從HttpWebResponse的Response流中讀出
            myStreamReader.Close();
            myResponseStream.Close();
            //Console.WriteLine(outdata);//顯示"登入"
            //拿到了Cookie,再進行請求就能直接讀取到登入後的內容了
            myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url2);
            myHttpWebRequest.CookieContainer = myCookieContainer;//*
            //剛才那個CookieContainer已經存有了Cookie,把它附加到HttpWebRequest中則能直接通過驗證
            myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
            myResponseStream = myHttpWebResponse.GetResponseStream();
            myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            outdata = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            //Response.Redirect(url2);
            //Console.WriteLine(outdata);
            //再次顯示"登入"
            //如果把*行注釋調,就顯示"沒有登入"
            string abc = outdata.Substring(413, outdata.Length - 413 - 11); //.Replace("title", "tag")
            //  abc = Regex.Replace(abc,"<!--User_Interface End-->.*?<!--Bottom-->" , "");//"title=\".*?\"|title='.*?'|<img.*?\">"
            Match mc = Regex.Match(abc, "<table>.*?/table>");//<div id=\"main\">.*?</div>  Regex.Matches(abc, "<table>.*?/table>")[0].Value
            abc = Regex.Replace(mc.Value, "<thead>.*?thead>", "");
         // MatchCollection results
 
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(abc );
            XmlNode xn = xmlDoc.SelectSingleNode("table");
            XmlElement xe = (XmlElement)xn;
            //xn.ChildNodes[0].ChildNodes[1].InnerText 
            //xn.ChildNodes.Count
            string result = xe.InnerText;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.