【问题标题】:How to POST request in c# with content-type being x-www-form-urlencoded?如何在 c# 中发布内容类型为 x-www-form-urlencoded 的请求?
【发布时间】:2017-06-17 15:52:49
【问题描述】:

Picture of Postman POST request

我想发出这个 POST 请求以接收 OAuth2 访问令牌,但在 Visual Studio 2015 中使用 c# 代码。

谢谢。

【问题讨论】:

    标签: c# oauth-2.0 http-post access-token postman


    【解决方案1】:

    System.Net.HttpWebRequest 是你需要知道的类,这是一个简单的示例。更多信息见MSDN:https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.110).aspx

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Method = "POST";
    webRequest.AllowAutoRedirect = true;
    webRequest.Timeout = 20 * 1000;
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
    webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";
    
    
    //write the data to post request
    //Postdata : a=1&b=2&c=3
    byte[] buffer = Encoding.Default.GetBytes(Postdata);
    if (buffer != null)
    {
         webRequest.ContentLength = buffer.Length;
         webRequest.GetRequestStream().Write(buffer, 0, buffer.Length);
    }
    
    WebResponse wr = webRequest.GetResponse()
    

    【讨论】:

    • 抱歉,目前无法使用。您将如何写出类型为 x-www-form-url-encoded 的 Postdata?作为字符串格式或字典或列表?感谢大家的帮助!
    • 见注释Postdata是一个字符串Postdata : a=1&b=2&c=3
    猜你喜欢
    • 2020-06-23
    • 2021-01-01
    • 1970-01-01
    • 2017-12-18
    • 2018-04-04
    • 2019-07-28
    • 1970-01-01
    • 2018-12-25
    相关资源
    最近更新 更多