简体   繁体   English

C#HttpWebRequest表单发布数据

[英]C# HttpWebRequest Form post data

In my C# Winform application I'm trying to send form post data to a web page of Instagram: 在我的C#Winform应用程序中,我试图将表单发布数据发送到Instagram的网页:

try
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.instagram.com/create/configure/");
    req.Method = "POST";

    if (req.CookieContainer == null)
        req.CookieContainer = new CookieContainer();

    foreach (System.Net.Cookie cookie in cookies)
    {
        req.CookieContainer.Add(cookie);
    }

    String postData = "upload_id=" + upload_id + "&caption=Wow&usertags=&custom_accessibility_caption=&retry_timeout=";
    var data = Encoding.UTF8.GetBytes(postData);


    req.ContentType = "application/x-www-form-urlencoded";
    req.ContentLength = data.Length;
    req.Accept = "*/*";
    req.Referer = "https://www.instagram.com/create/details/";
    req.Headers["accept-encoding"] = "gzip, deflate, br";
    req.Headers["accept-language"] = "en-US,en;q=0.9";
    req.Headers["origin"] = "https://www.instagram.com";
    req.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A403 Safari/602.1";

    req.Headers["x-requested-with"] = "XMLHttpRequest";
    req.Headers["x-ig-app-id"] = "1217981644879628";
    req.Headers["x-csrftoken"] = csrftoken;
    req.Headers["x-instagram-ajax"] = rolloutHash;

    using (Stream requestStream = req.GetRequestStream())
    {
        requestStream.Write(data, 0, data.Length);
        requestStream.Close();
    }

    string source;
    using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
    {
        source = reader.ReadToEnd();
    }

    return source;
}
catch (Exception e)
{
    int t = 0;
    t++;
}

return "";

But I'm always getting 400 Exception from it. 但是我总是从中得到400 Exception。 Any idea what can be the problem? 知道可能是什么问题吗?

You are probably using an old version of api documentation, currently instagram api allows very limited actions. 您可能正在使用旧版的api文档,当前instagram api允许的操作非常有限。 check this 检查这个

https://www.instagram.com/developer/endpoints/ https://www.instagram.com/developer/endpoints/

and this 和这个

https://developers.facebook.com/docs/instagram-api/reference/media https://developers.facebook.com/docs/instagram-api/reference/media

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM