内容类型为application/x-www-form-urlencoded的Http post请求

内容类型为application/x-www-form-urlencoded的Http post请求,post,httpclient,access-token,url-encoding,Post,Httpclient,Access Token,Url Encoding,如何发送内容类型为application/x-www-form-urlencoded的POST请求。 给定一个访问代码,我尝试使用POST请求获取AccessToken,在POST URL中设置了所有信息,因此我不知道在postAysnc方法中作为Http内容传递什么 根据application/x-www-form-urlencoded的另一篇文章,发送到服务器的HTTP消息的主体本质上是一个巨大的查询字符串——名称/值对用符号(&)分隔,名称与值用等号(=)分隔。这方面的一个例子是: MyV

如何发送内容类型为application/x-www-form-urlencoded的POST请求。 给定一个访问代码,我尝试使用POST请求获取AccessToken,在POST URL中设置了所有信息,因此我不知道在postAysnc方法中作为Http内容传递什么

根据application/x-www-form-urlencoded的另一篇文章,发送到服务器的HTTP消息的主体本质上是一个巨大的查询字符串——名称/值对用符号(&)分隔,名称与值用等号(=)分隔。这方面的一个例子是:

MyVariableOne=ValueOne&MyVariableTwo=ValueTwo

因此,我有一个类似的例子,我的POST url将所有信息都作为querystring,在这种情况下,我不知道在postAysnc方法中作为HttpContent传递什么,因为它是一个强制参数

HttpClient=新的HttpClient(); StringContent queryString=新的StringContent(数据); HttpResponseMessage response=wait client.PostAsync(新Uri(url),queryString)

有两种选择:

  • 继续使用
    StringContent
    ,但设置内容类型:
    newstringcontent(data,Encoding.UTF8,“application/x-www-form-urlencoded”)
  • 不要使用
    StringContent
    ,而是使用
    FormUrlEncodedContent
    ;注意,这将采用描述值的
    KeyValuePair
    序列(例如,创建一个包含
    {{“MyVariableOne”、“ValueOne”}、{“MyVariableTwo”、“ValueTwo”}}