试图登录网站C#

时间:2018-04-16 22:32:27

标签: c# post login get

我的代码工作并且第一次成功登录我,但是当我第二次尝试时,表单冻结,我必须重新启动程序。这是我的代码:

private void button1_Click(object sender, EventArgs e)
    {
        Auth(textBox1.Text, textBox2.Text);
    }

    private void Auth(string user, string pass)
    {
        try
        {
            //GET CSRF TOKEN
            var request = (HttpWebRequest)WebRequest.Create("https://accounts.spotify.com/en-US/login?continue=https:%2F%2Fwww.spotify.com%2Fus%2Faccount%2Foverview%2F");

            request.Method = "GET";
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36";
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";

            var response1 = (HttpWebResponse)request.GetResponse();
            var headers = response1.Headers;

            var csrf = headers["Set-Cookie"].Split('=', ';')[1];

            //POST LOGIN
            var request2 = (HttpWebRequest)WebRequest.Create("https://accounts.spotify.com/api/login");

            var postdata = Encoding.ASCII.GetBytes(string.Format("remember=true&username={0}&password={1}&captcha_token=&csrf_token={2}", user, pass, csrf));
            request2.Method = "POST";
            request2.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36";
            request2.ContentType = "application/x-www-form-urlencoded";
            request2.ContentLength = postdata.Length;
            request2.Headers.Add(string.Format("Cookie: sp_t=4c114efa38d7c3d8510fa41bb7fd76af; spot=%7B%22t%22%3A1523485160%2C%22m%22%3A%22us%22%2C%22p%22%3Anull%7D; _ga=GA1.2.981430147.1523485160; sp_usid=919af976-2997-44dc-872b-1704356316bb; sp_last_utm=%7B%22utm_source%22%3A%22us-en_brand_contextual-desktop_text%22%2C%22utm_medium%22%3A%22paidsearch%22%2C%22utm_campaign%22%3A%22alwayson_ucanz_us_performancemarketing_core_brand+contextual-desktop+text+bmm+us-en+google%22%7D; _gid=GA1.2.411374915.1523901844; _gcl_aw=GCL.1523901845.CjwKCAjwk9HWBRApEiwA6mKWabkt4w1SE6lOm5TjrId-HkZNuakJeNIhOAkuVou7Vmueqb0bbw7sUBoCcvYQAvD_BwE; _gcl_dc=GCL.1523901845.CjwKCAjwk9HWBRApEiwA6mKWabkt4w1SE6lOm5TjrId-HkZNuakJeNIhOAkuVou7Vmueqb0bbw7sUBoCcvYQAvD_BwE; _gac_UA-5784146-31=1.1523902416.CjwKCAjwk9HWBRApEiwA6mKWabkt4w1SE6lOm5TjrId-HkZNuakJeNIhOAkuVou7Vmueqb0bbw7sUBoCcvYQAvD_BwE; _gat_UA-5784146-31=1; _gali=header-login-link; __bon=MHwwfC0zMTA4ODYyMDR8LTEzMDU3MjIwNTY4fDF8MXwxfDE=; fb_continue=https%3A%2F%2Fwww.spotify.com%2Fus%2Faccount%2Foverview%2F; _gat=1; csrf_token={0}; remember=ghyuit78%40gmail.com", csrf));

            using (Stream w = request2.GetRequestStream()) //Freezes around here the second time around
            {
                w.Write(postdata, 0, postdata.Length);
            }

            var response2 = new StreamReader(((HttpWebResponse)request2.GetResponse()).GetResponseStream()).ReadToEnd();

            richTextBox1.Text = "Request1:\n----------------------\n" + response2;

            if (response2.Contains("displayName"))
            {
                label3.ForeColor = Color.Green;
                label3.Text = "Successfully logged in!";
            }
            else
            {
                label3.ForeColor = Color.Red;
                label3.Text = "Invalid username/password. Try again!";
            }
        }
        catch
        {
            label3.ForeColor = Color.Red;
            label3.Text = "ERROR! Try again!";
        }

    }

我设法第一次成功登录,但是当我再次按下按钮时,它会冻结并且不会给我一个错误或任何东西。我必须打开我的任务管理器并结束这个过程。

0 个答案:

没有答案