2011-04-19 47 views
0

我想从论坛下载一些数据。包含数据的页面仅对注册用户可见。以下是包含用户数据的示例网页;在用户名/密码验证背后提取数据

http://www.bikeforums.net/member.php/227664-StackOverflow

我想获得使用wget或C#中的数据。我尝试通过Firefox登录,然后将cookie文件(希望包含登录信息)传递给wget。这更像是一场临时的黑客攻击,并非真正的解决方案,但仍然失败。我如何正确地做到这一点?

我建立了一个帐户进行测试,如果这是有帮助的。

用户:StackOverflow上

通行证:so123

回答

0

使用Firebug,你可以很容易地得到POST数据登录页面,并使用它来创建一个WebRequest的,并登录该论坛。

服务器创建用于身份验证的cookie,我们可以在论坛页面的下一个请求中使用此cookie,以便服务器可以验证请求并返回所有数据。

在这里,我测试了一个简单的控制台应用程序,实现这种机制。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Globalization; 
using System.Web; 
using System.Net; 
using System.Xml; 
using System.Xml.XPath; 
using System.Xml.Linq; 
using System.IO; 



namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      CookieContainer cookieContainer = new CookieContainer(); 
      HttpWebRequest wpost = (HttpWebRequest) HttpWebRequest.Create("http://www.bikeforums.net/login.php?do=login"); 
      wpost.CookieContainer = cookieContainer; 
      wpost.Method = "POST"; 
      string postData = "do=login&vb_login_md5password=d93bd4ce1af6a9deccaf0ea844d6c05d&vb_login_md5password_utf=d93bd4ce1af6a9deccaf0ea844d6c05d&s=&securitytoken=guest&url=%2Fmember.php%2F227664-StackOverflow&vb_login_username=StackOverflow&vb_login_password="; 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
      // Set the ContentType property of the WebRequest. 
      wpost.ContentType = "application/x-www-form-urlencoded"; 
      // Set the ContentLength property of the WebRequest. 
      wpost.ContentLength = byteArray.Length; 
      // Get the request stream. 
      System.IO.Stream dataStream = wpost.GetRequestStream(); 
      // Write the data to the request stream. 
      dataStream.Write(byteArray, 0, byteArray.Length); 
      // Close the Stream object. 
      dataStream.Close(); 
      // Get the response. 
      HttpWebResponse response = (HttpWebResponse) wpost.GetResponse(); 

      // Request 
      wpost = (HttpWebRequest)WebRequest.Create("http://www.bikeforums.net/member.php/227664-StackOverflow"); 

      //Assing the cookies created on the server to the new request 
      wpost.CookieContainer = cookieContainer; 
      wpost.Method = "GET"; 
      response = (HttpWebResponse)wpost.GetResponse(); 

      Stream receiveStream = response.GetResponseStream(); 
      // Pipes the stream to a higher level stream reader with the required encoding format. 
      StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); 
      //Display the result to console... 
      Console.WriteLine(readStream.ReadToEnd()); 
      response.Close(); 
      readStream.Close(); 

      Console.Read(); 

     } 
    } 
}