C# 自動登入網頁,瀏覽頁面【轉載】

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   strong   

需求:客戶的資料同時存在在另外一個不可控的系統中,需要和當前系統同步。

思路:自動登入另外一個系統,然後抓取資料,同步到本系統中。

技術點:類比使用者登入;儲存登入狀態;抓取資料

/// <summary>        /// visit the target url        /// </summary>        /// <param name="targetURL"></param>        /// <param name="cc">this is for keeping cookies and sessions</param>        /// <param name="param">this is the data need post inside form</param>        /// <returns>html page</returns>        public static string PostAndGetHTML(string targetURL,CookieContainer cc,Hashtable param)        {            //prepare the submit data            string formData = "";            foreach (DictionaryEntry de in param)            {                formData += de.Key.ToString() + "=" + de.Value.ToString() + "&";            }            if (formData.Length > 0)                formData = formData.Substring(0, formData.Length - 1); //remove last ‘&‘            ASCIIEncoding encoding = new ASCIIEncoding();            byte[] data = encoding.GetBytes(formData);            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);            request.Method = "POST";    //post            request.ContentType = "application/x-www-form-urlencoded";            request.ContentLength = data.Length;            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";            Stream newStream = request.GetRequestStream();            newStream.Write(data, 0, data.Length);            newStream.Close();            request.CookieContainer = cc;            HttpWebResponse response = (HttpWebResponse)request.GetResponse();            cc.Add(response.Cookies);            Stream stream = response.GetResponseStream();            string result = new StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();            return result;        }        public static DataTable ConvertToDT(DataTable dt, string tableHTML)        {            int lastTD = tableHTML.ToLower().LastIndexOf("</td>");            int firstRow = tableHTML.ToLower().IndexOf("<tr") + 3;//after ""<tr            int index = tableHTML.ToLower().IndexOf("<tr", firstRow) + 3;//after ""<tr            while (index < lastTD)            {                DataRow dr = dt.NewRow();                for (int i = 0; i < dt.Columns.Count; i++)                {                    string value = "";                    int startTD = tableHTML.ToLower().IndexOf("<td", index) + 3;//after "<td"                    int endTD = tableHTML.ToLower().IndexOf("</td>", startTD);                    if (endTD < 0)                        break;                    string tdStr = tableHTML.Substring(startTD, endTD - startTD);                    //remove <> and others                    tdStr = tdStr.Replace("&nbsp;", "").Replace("\t", "").Replace("\r", "");                    string[] v = tdStr.Split(‘<‘, ‘>‘);                    for (int j = 0; j < v.Length; j++)                    {                        j++;                        if (v[j].Trim() != "")                        {                            value = v[j].Trim();                            break;                        }                    }                    //                    dr[i] = value;                    index = endTD;                }                dt.Rows.Add(dr);            }            return dt;        }

 這一個是調用的例子:先登入,在查詢。 實際中這個邏輯可能有很多步驟

CookieContainer cc = new CookieContainer();//this is for keep the Session and Cookie            Hashtable param = new Hashtable();//this is for keep post data.            string urlLogin = "http://demo.server//login.asp";            //do find the elementId that needed. check the source of login page can get this information            param.Add("User", "xxx");            param.Add("Password", "xxxx");            string result =GrabHelper.PostAndGetHTML(urlLogin, cc, param);            //check result, whether login success            //if login success, goto the target url, and input some value.            string url2 = " http://demo.server/query.asp?id=1";// need change. special logic            param.Clear();            //param.Add("SearchAreaId","JobId")            result = GrabHelper.PostAndGetHTML(url2, cc, new Hashtable());            //ConvertToDT the html or do something others

C# 自動登入網頁,瀏覽頁面【轉載】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.