当前位置: 代码迷 >> ASP.NET >> 小弟跪求:怎么用程序自动登录网站并获取指定页面的信息
  详细解决方案

小弟跪求:怎么用程序自动登录网站并获取指定页面的信息

热度:3571   发布时间:2013-02-25 00:00:00.0
小弟跪求:如何用程序自动登录网站并获取指定页面的信息?
我想用程序自动登录到某个网站获取它其中某个页面的邮件信息,可是总是停在登录页面,没有验证码,可就是进不去,我知道需要用httpwebrequest,也试了网上的代码可总不得窍门,总实现不了,有什么办法多赐教,能贴代码更好。

初学asp.net,望高手们不吝赐教。

------解决方案--------------------------------------------------------
给你一个登录校内的代码。
C# code
 public static bool login(string user, string passwd,bool isuseproxy,WebProxy proxy)        {            string text1 = "http://login.xiaonei.com/Login.do";            string[] textArray1 = new string[5] { "email=", user, "&password=", passwd, "&origURL=http%3A%2F%2Fwww.xiaonei.com%2FSysHome.do&submit=%E7%99%BB%E5%BD%95" };            string text2 = string.Concat(textArray1);            HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create(text1);            request1.Method = "POST";            request1.KeepAlive = true;            request1.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1";            request1.ContentType = "application/x-www-form-urlencoded";            request1.AllowAutoRedirect = true;            if (proxy != null && isuseproxy != false)            {                request1.Proxy = proxy;            }                        request1.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";            CookieContainer container1 = new CookieContainer();            request1.CookieContainer = container1;            Stream stream1 = request1.GetRequestStream();            StreamWriter writer1 = new StreamWriter(stream1, Encoding.ASCII);            writer1.Write(text2);            writer1.Close();            stream1.Close();            HttpWebResponse response1 = (HttpWebResponse)request1.GetResponse();            Program.cookie = request1.CookieContainer.GetCookieHeader(new Uri("http://login.xiaonei.com/Login.do"));            Encoding encoding1 = Encoding.GetEncoding("utf-8");            Stream stream2 = response1.GetResponseStream();            StreamReader reader1 = new StreamReader(stream2, encoding1);            StringBuilder builder1 = new StringBuilder();            while (reader1.Peek() != -1)            {                builder1.Append(reader1.ReadLine());            }            string text3 = builder1.ToString();            request1.Abort();            Match match1 = Regex.Match(text3, "<title>.*</title>");            if ((match1.Length >= 1) && (match1.Value != "<title>\u6821\u5185\u7f51 - \u767b\u5f55</title>"))            {                Match match2 = Regex.Match(text3, "id=[0-9]{8,9}.*\u9996\u9875");                if (match2.Length >= 1)                {                    Match match3 = Regex.Match(match2.Value, "[0-9]{8,9}");                    if (match3.Length >= 1)                    {                        Program.selfID = match3.Value;                    }                }                return true;            }            return false;        }        public static WebProxy setWebPorxy(string Uri, int port, string uid, string pwd)        {            WebProxy myProxy = new WebProxy(Uri, port);            try            {                if (Uri.Length > 0)                {                    myProxy.Credentials = new NetworkCredential(uid, pwd,"acc");                }                else                {                    return null;                }            }            catch            {                return null;            }            return myProxy;        }
  相关解决方案