Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Login to Instagram via webrequest and recover the cookie in C#

So i have wanted to create an application in C# that sends a webrequest with password/username to the Instagram server to then get an auth cookie back. So far i have read a lot of post and couldnt find an answer to my problem and nothing really worked. The last thing i tried was a post another user made here with a similar problem but i only get an error "403 Forbidden" from the remoteserver code below:

        string userName = "user";
        string password = "pass";

        ASCIIEncoding encoding = new ASCIIEncoding();
        string postData = "username=" + userName + "&password=" + password;
        byte[] postDataBytes = encoding.GetBytes(postData);

        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.instagram.com/accounts/login/");

        httpWebRequest.Method = "POST";
        httpWebRequest.ContentType = "application/x-www-form-urlencoded";
        httpWebRequest.ContentLength = postDataBytes.Length;
        httpWebRequest.AllowAutoRedirect = false;

        using (var stream = httpWebRequest.GetRequestStream())
        {
            stream.Write(postDataBytes, 0, postDataBytes.Length);
            stream.Close();
        }

        var cookieContainer = new CookieContainer();

        using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
        {
            using (var streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
            {
                foreach (Cookie cookie in httpWebResponse.Cookies)
                {
                    cookieContainer.Add(cookie);
                }
            }
        }

        MessageBox.Show(cookieContainer.ToString());

Comments