2013-07-19 25 views
1

你好,我正在試圖開發一個像自動機器人一樣的Facebook自動化。我可以登錄Facebook,並可以在登錄時導航到照片。但是,我怎樣才能點擊照片上的按鈕?誰能幫忙?如何在登錄Facebook後點擊超鏈接?

這是我的代碼。

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Net; 
    using System.IO; 

    namespace ConsoleApplication1 
    { 
     class Program 
     { 
      static void Main(string[] args) 
      { 
       //Get the response from the server and save the cookies from the first request.. 
      CookieCollection cookies = new CookieCollection(); 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.facebook.com"); 
      request.CookieContainer = new CookieContainer(); 
      request.CookieContainer.Add(cookies); 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      cookies = response.Cookies; 


      //Use cookieee 
      string getUrl = "http://www.facebook.com/login.php?login_attempt=1"; 
      string postData = String.Format("email={0}&pass={1}", "[email protected]", "password"); 
      HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl); 
      getRequest.CookieContainer = new CookieContainer(); 
      getRequest.CookieContainer.Add(cookies); //recover cookies First request 
      getRequest.Method = WebRequestMethods.Http.Post; 
      getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; 
      getRequest.AllowWriteStreamBuffering = true; 
      getRequest.ProtocolVersion = HttpVersion.Version11; 
      getRequest.AllowAutoRedirect = true; 
      getRequest.ContentType = "application/x-www-form-urlencoded"; 
      CookieContainer container = new CookieContainer(); 
      container = getRequest.CookieContainer; 

      byte[] byteArray = Encoding.ASCII.GetBytes(postData); 
      getRequest.ContentLength = byteArray.Length; 
      Stream newStream = getRequest.GetRequestStream(); //open connection 
      newStream.Write(byteArray, 0, byteArray.Length); // Send the data. 
      newStream.Close(); 
      HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse(); 



      //Now make like...but not working... 
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://m.facebook.com/a/like.php?perm&ft_ent_identifier=495210693893173&gfid=AQAM3G9owHcMQG4B"); 
      req.CookieContainer = container; 
      req.Method = "POST"; 
      //req.ContentType = "application/x-www-form-urlencoded"; 
      req.KeepAlive = true; 
      try 
      { 
       HttpWebResponse res = (HttpWebResponse)req.GetResponse(); 
       StreamReader sr = new StreamReader(res.GetResponseStream()); 
       string source = sr.ReadToEnd(); 

       StreamWriter myWriter = File.CreateText(@"D:\\test.txt"); 
       myWriter.Write(source); 
       myWriter.Close(); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.ToString()); 
      } 

      } 
     } 
    } 

`

回答

0

希望這將間接地回答你的問題 還有就是我用來做相同的功能是Firefox的插件。 我最終編寫了一個腳本來自動調用,以便在發生戰爭時可以使用。

我不確定你的目標是自己編程,還是隻是爲了實現功能。 http://www.iopus.com/imacros/firefox/

http://wiki.imacros.net/Sample_Code

我的建議是看iMacros的是怎麼做的,以及在螢火拉起來。 也看看他們的示例代碼,看看他們是如何實現相同的效果。