.Net Core将表单数据发送到uri

big*_*ter 3 asp.net-core

您如何将表单数据发布到.Net Core中的外部URL?例如,如果我想重新创建此示例请求:

POST 
Url: www.googleapis.com/oauth2/v4/token
Content-Type: application/x-www-form-urlencoded

code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=ABCD1234&
client_secret=XYZ1234&
redirect_uri=https://test.example.com/code&
grant_type=authorization_code
Run Code Online (Sandbox Code Playgroud)

我在这里找到了一个示例,该示例显示了发布JSON的示例,但没有表单数据的示例。

tpe*_*zek 7

这是可以做到HttpClientMicrosoft.AspNet.WebApi.Client包以及通过使用FormUrlEncodedContent

IList<KeyValuePair<string, string>> nameValueCollection = new List<KeyValuePair<string, string>> {
    { new KeyValuePair<string, string>("code", "4/P7q7W91a-oMsCeLvIaQm6bTrgtp7") },
    { new KeyValuePair<string, string>("client_id", "ABCD1234") },
    { new KeyValuePair<string, string>("client_secret", "XYZ1234") },
    { new KeyValuePair<string, string>("redirect_uri", "https://test.example.com/code") },
    { new KeyValuePair<string, string>("grant_type", "authorization_code") },
};

client.PostAsync("www.googleapis.com/oauth2/v4/token", new FormUrlEncodedContent(nameValueCollection));
Run Code Online (Sandbox Code Playgroud)