2012-12-24 137 views
3

我正在嘗試使用網絡請求登錄instagram。我很難理解發生了什麼事。得到這個:遠程服務器返回一個錯誤:(403)禁止。我至今:以編程方式登錄Instagram

public static string csrf; 
    CookieContainer c1 = new CookieContainer(); 

    private void button1_Click(object sender, EventArgs e) 
    { 
     string PostData = String.Format("csrfmiddlewaretoken={0}&username=ra123&password=ra12345678",getToken()); 

     HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://instagram.com/accounts/login/"); 
     req.Method = "POST"; 
     req.ContentType = "application/x-www-form-urlencoded"; 
     req.KeepAlive = true; 
     req.AllowAutoRedirect = true;    
     req.CookieContainer = c1; 
     byte[] byteArray = Encoding.ASCII.GetBytes(PostData); 
     req.ContentLength = byteArray.Length; 
     Stream dataStream = req.GetRequestStream(); 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     dataStream.Flush(); 
     dataStream.Close(); 

      HttpWebResponse webResp = (HttpWebResponse)req.GetResponse(); 
      Stream datastream = webResp.GetResponseStream(); 
      StreamReader reader = new StreamReader(datastream); 
      string s = reader.ReadToEnd(); 
      MessageBox.Show(s); 
      if (s.Contains("ra123")) 
      { 

       MessageBox.Show("Loggedin"); 

      } 
      else 
      { 
       MessageBox.Show("Not"); 
      } 

    } 


    string getToken() 
    { 
     string p = "<input type=\"hidden\" name=\"csrfmiddlewaretoken\" value=\"(.*)\"/>"; 
     HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://instagram.com/accounts/login/"); 
     req.Method = "GET"; 
     req.CookieContainer = c1; 

     HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); 
     Stream data = resp.GetResponseStream(); 
     StreamReader sr = new StreamReader(data); 
     string src = sr.ReadToEnd(); 

     Match m = Regex.Match(src, p); 
     if (m.Success) 
     { 
      return (m.Groups[1].Value.ToString()); 
     } 
     return false.ToString(); 
    } 

回答

0

FYI,這不會解決你的問題,但你需要學習的地方實現IDisposableusing塊你Stream和其他對象:

public static string csrf; 
CookieContainer c1 = new CookieContainer(); 

private void button1_Click(object sender, EventArgs e) 
{ 
    string PostData = String.Format("csrfmiddlewaretoken={0}&username=ra123&password=ra12345678", getToken()); 

    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://instagram.com/accounts/login/"); 
    req.Method = "POST"; 
    req.ContentType = "application/x-www-form-urlencoded"; 
    req.KeepAlive = true; 
    req.AllowAutoRedirect = true; 
    req.CookieContainer = c1; 
    byte[] byteArray = Encoding.ASCII.GetBytes(PostData); 
    req.ContentLength = byteArray.Length; 
    using (Stream dataStream = req.GetRequestStream()) 
    { 
     dataStream.Write(byteArray, 0, byteArray.Length); 
     dataStream.Flush(); 
     dataStream.Close(); 
    } 

    string s; 
    using (HttpWebResponse webResp = (HttpWebResponse)req.GetResponse()) 
    { 
     using (Stream datastream = webResp.GetResponseStream()) 
     { 
      using (StreamReader reader = new StreamReader(datastream)) 
      { 
       s = reader.ReadToEnd(); 
      } 
     } 
    } 
    MessageBox.Show(s); 
    if (s.Contains("ra123")) 
    { 

     MessageBox.Show("Loggedin"); 

    } 
    else 
    { 
     MessageBox.Show("Not"); 
    } 

} 

string getToken() 
{ 
    string p = "<input type=\"hidden\" name=\"csrfmiddlewaretoken\" value=\"(.*)\"/>"; 
    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://instagram.com/accounts/login/"); 
    req.Method = "GET"; 
    req.CookieContainer = c1; 

    string src; 
    using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) 
    { 
     using (Stream data = resp.GetResponseStream()) 
     { 
      using (StreamReader sr = new StreamReader(data)) 
      { 
       src = sr.ReadToEnd(); 
      } 
     } 
    } 

    Match m = Regex.Match(src, p); 
    if (m.Success) 
    { 
     return (m.Groups[1].Value.ToString()); 
    } 
    return false.ToString(); 
} 
+0

這對我有幫助嗎?我的意思是「使用」塊有什麼好處。此外,我真的很感激這一點,但我現在只需要解決我的問題:( – user1815324

+0

這就是爲什麼我說它不會解決這個問題。 –

+0

@JohnSaunders不會讓這不是一個答案嗎? – bmargulies

1

的登錄的問題是請求需要在標頭設置cookie,並且容器沒有設置它,因爲當您從未知資源管理器訪問時,每次登錄都會發生更改。以下是你可以做的事情:

WebResponse Response; 
HttpWebRequest Request; 
Uri url = new Uri("http://thewebpage.com:port/login/"); 

CookieContainer cookieContainer = new CookieContainer(); 

Request = (HttpWebRequest)WebRequest.Create(url); 
Request.Method = "GET"; 
Request.CookieContainer = cookieContainer; 

// Get the first response to obtain the cookie where you will find the "csrfmiddlewaretoken" value 
Response = Request.GetResponse(); 

string Parametros = "csrfmiddlewaretoken=" + cookieContainer.GetCookies(url)["csrftoken"].Value + "&username=USER&password=PASSWORD&next="; // This whill set the correct url to access 

Request = (HttpWebRequest)WebRequest.Create(url); // it is important to use the same url used for the first request 
Request.Method = "POST"; 
Request.ContentType = "application/x-www-form-urlencoded"; 
Request.UserAgent = "Other"; 
// Place the cookie container to obtain the new cookies for further access 
Request.CookieContainer = cookieContainer; 
Request.Headers.Add("Cookie",Response.Headers.Get("Set-Cookie")); // This is the most important step, you have to place the cookies at the header (without this line you will get the 403 Forbidden exception             

byte[] byteArray = Encoding.UTF8.GetBytes(Parametros); 
Request.ContentLength = byteArray.Length; 

Stream dataStream = Request.GetRequestStream(); 
dataStream.Responseite(byteArray, 0, byteArray.Length); 
dataStream.Close(); 

Response = Request.GetResponse();