[.net]模拟网站登陆、截取数据

来源:互联网 发布:linux系统可以ghost 编辑:程序博客网 时间:2024/04/30 02:09

经常在网上见到一些广告发布工具,这种工具主要是针对一些比较热门或在搜索引擎搜索到的网站自动发布广告信息。
今天研究了下类似这种工具的实现方法,其实并不是很深奥的东西。当然,我的目的不是做这个,只是做个模拟用户操作:用户登陆到信息发布的功能全自动化而已。

首先,需要一个工具(当然可以不要,不过要自己一个个的揍提交的数据):
05_170500_httplook.rar

这个工具很好使,可以挂着IE检索到网站的HTTP头及内容。(使用方法是先运行软件,工具栏上点绿色的start按钮,再运行IE打开网站,会自动截取所有数据包)

说明:以下是代码全为C#实现

下面是取得网站Cookie的方法:

1 ///    2 /// 取得网站的Cookie   3 ///    4 ///  网站地址   5 ///  你要Post的数据包   6 ///    7 public static IList<string> getCookie( string URL, string paramsList ) {   8     CookieContainer myCookieContainer = new CookieContainer();   9     //新建一个CookieContainer来存放Cookie集合    10     HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create( URL );   11     //新建一个HttpWebRequest    12     myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";   13     myHttpWebRequest.ContentLength = paramsList.Length;   14     myHttpWebRequest.Method = "POST";   15     myHttpWebRequest.CookieContainer = myCookieContainer;   16     //设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer    17     Stream myRequestStream = myHttpWebRequest.GetRequestStream();   18     StreamWriter myStreamWriter = new StreamWriter( myRequestStream, Encoding.GetEncoding( "gb2312" ) );   19     myStreamWriter.Write( paramsList );   20     //把数据写入HttpWebRequest的Request流    21     myStreamWriter.Close();   22     myRequestStream.Close();   23     //关闭打开对象    24     HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();   25     //新建一个HttpWebResponse    26     myHttpWebResponse.Cookies = myCookieContainer.GetCookies( myHttpWebRequest.RequestUri );   27   28     IList<string> listCookies = new List<string>();   29     for ( int i = 0;i < myHttpWebResponse.Cookies.Count;i++ ) {   30         listCookies.Add( myHttpWebResponse.Cookies[i].ToString() );   31     }   32     myHttpWebResponse.Close();   33     return listCookies;   34 }    view plain | print | copy to clipboard | ?

使用WebClient对象加上面的方法就可以做很多事情了。这里说明一下:
登陆网站后最重要的就是要得到网站的Cookie。我们知道,在写后台程序的时候几乎所有程序类型(包括ASP、PHP、JSP、ASP.net)都要用到Session,而这个Session在浏览器中则是以Cookie形式发送的。而浏览器接收到的这个Cookie只是一个“句柄”,且非文件格式存于浏览中的。那么要得到这一个值,就要用到上面的那工具了。其它的文件格式存放的Cookie,可以在浏览器地址栏中键入看到:
javascript:alert(document.cookie);

下面贴出示例代码段:

1 //以下为网站登陆页面   2 string urlLogin = "http://localhost/webtest/Default.aspx";   3 //以下为要访问的页面   4 string urlPage = "http://localhost/webtest/Test2.aspx";   5 //以下为要传给登陆页面的数据包(这里是ASP.net的,有区别于其它程序)   6 string postData = "__VIEWSTATE=%2FwEPDwULLTExNTc2NTI3OTlkZCM%2FicbUHtc%2FphGIcdstb36b8f2S&TextBox1=zxb&TextBox2=bxz&Button1=Button&__EVENTVALIDATION=%2FwEWBAK1lvnKDQLs0bLrBgLs0fbZDAKM54rGBrGiTUxPf6PrEHKYZSJgZj5Q5xUn";   7 IList<string> listCookies = getCookie( urlLogin, postData );   8 WebClient wcBrower = new WebClient();   9   10 wcBrower.Headers.Add( "Content-Type""application/x-www-form-urlencoded" );   11 wcBrower.Headers.Add( "Cookie", listCookies[0] );   12   13 // Apply ASCII Encoding to obtain the string as a byte array.   14 byte[] byteArray = Encoding.ASCII.GetBytes( postData );   15 Console.WriteLine( "Uploading to {0} ...", urlPage );   16 // Upload the input string using the HTTP 1.0 POST method.   17 byte[] responseArray = wcBrower.DownloadData( urlPage );   18   19 // Decode and display the response.   20 Console.WriteLine( "/nResponse received was {0}", Encoding.ASCII.GetString( responseArray ) );    view plain | print | copy to clipboard | ?

WebClient类的功能很强大,不仅有下载数据的方法,可提供上传的方法,最重要的是它相当于一个浏览器,登陆过后会保Cookie在内存中。

有空再研究一下,想毕可以写很多东东出来(比如灌水机器人之类的)。呵!