以编程方式登录instagram [英] Login to instagram programmatically

查看:360
本文介绍了以编程方式登录instagram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Web请求登录instagram.我很难理解发生了什么事.获取此信息:远程服务器返回错误:(403)禁止.到目前为止,我所拥有的:

I am trying to log in to instagram using web requests. I am having a bad time understanding what's going on. Getting this: The remote server returned an error: (403) Forbidden. What I have so far:

    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();
    }

推荐答案

登录问题是请求需要在标题处设置cookie,而容器没有设置它,因为每次登录时都会更改来自未知浏览器的访问.这是您可以做的:

The problem with the login is that the request needs to set the cookie at the header, and the container is not setting it since is changes at every login when you access from an unknown explorer. Here is what you can do:

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();

这篇关于以编程方式登录instagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆