C# 403向Instagram提交post请求时的错误响应
我试图通过HTTP post请求“以编程方式”登录Instagram。尽管如此,每当我尝试对这个URL执行此操作时:-它会给我一个404错误。但是,如果我从末尾删除斜杠,例如/accounts/login,那么它将起作用,但是,响应主体似乎只是一个简单的GET请求,因为它们只是简单地输出相同的GET请求。我实际上期待一条错误消息作为响应 代码是用C#编写的,没有什么特别之处;基本上是http,然后我在TCP流中写入post数据。该网站正在使用CSRF令牌,该令牌需要包含在post请求中,因此我首先使用一个简单的GET请求获取该密钥,然后继续发布 我有没有遗漏任何技术方面的东西?我已经尝试过这段代码,在其他几个网站上也做过同样的尝试,所有的尝试都是成功的 代码看起来很像这样(同样的问题):C# 403向Instagram提交post请求时的错误响应,c#,https,C#,Https,我试图通过HTTP post请求“以编程方式”登录Instagram。尽管如此,每当我尝试对这个URL执行此操作时:-它会给我一个404错误。但是,如果我从末尾删除斜杠,例如/accounts/login,那么它将起作用,但是,响应主体似乎只是一个简单的GET请求,因为它们只是简单地输出相同的GET请求。我实际上期待一条错误消息作为响应 代码是用C#编写的,没有什么特别之处;基本上是http,然后我在TCP流中写入post数据。该网站正在使用CSRF令牌,该令牌需要包含在post请求中,因此我首
提前谢谢 如果您在问题中包含代码,您可能会得到更好的答案。是的,代码现在已包含。
“&username=USER&password=password&next=“
在您的代码中包含正确的组合?此外,您是否手头有“支持instagram”的设备,以便您可以检查发送的邮件头,例如使用?
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