我正在尝试“以编程方式”使用HTTP发布请求登录Instagram。虽然,每当我尝试对此网址执行此操作时:https://instagram.com/accounts/login/ - 它会给我404错误。但是,如果我从末尾删除斜杠,例如/ accounts / login,然后它会工作,但是,响应体似乎只是一个简单的GET请求,因为它们只是输出相同的GET请求。我实际上期待一条错误消息作为回应。
代码是用C#编写的,没什么特别的;基本上是一个http web request,然后我在TCP流中写了帖子数据。该网站正在使用CSRF令牌,该令牌需要包含在发布请求中,因此我首先使用简单的GET请求获取此密钥,然后继续POST。
我缺少哪些技术方面?我已经尝试过代码并在其他几个网站上做了同样的事情,所有的尝试都是成功的。
代码看起来很像(同样的问题):
WebResponse Response;
HttpWebRequest Request;
Uri url = new Uri("https://instagram.com/accounts/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.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
Response = Request.GetResponse(); // Fails here
提前致谢!