FormUrlEncodedContent
FormUrlEncodedContent из пары
Name="abcde" даёт текст
"Name=abcde". Две пары
соединяются через &.
Пробел в значении стал бы "%20" -
в примерах пробел не используется.
Синтаксис
var pairs = new[] { new KeyValuePair<string, string>("Name", "abcde") };
var content = new FormUrlEncodedContent(pairs);
Пример
Давайте прочитать одну пару:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var pairs = new[]
{
new KeyValuePair<string, string>("Name", "abcde")
};
var content = new FormUrlEncodedContent(pairs);
string res = await content.ReadAsStringAsync();
Console.WriteLine(res);
}
}
Результат выполнения кода:
"Name=abcde"
Пример
Давайте прочитать две пары:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
var pairs = new[]
{
new KeyValuePair<string, string>("Name", "abcde"),
new KeyValuePair<string, string>("Count", "12345")
};
var content = new FormUrlEncodedContent(pairs);
string res = await content.ReadAsStringAsync();
Console.WriteLine(res);
}
}
Результат выполнения кода:
"Name=abcde&Count=12345"
Пример
Давайте проверить тип содержимого:
using System;
using System.Collections.Generic;
using System.Net.Http;
class Program
{
static void Main()
{
var pairs = new[]
{
new KeyValuePair<string, string>("Name", "abcde")
};
var content = new FormUrlEncodedContent(pairs);
string res = content.Headers.ContentType.MediaType;
Console.WriteLine(res);
}
}
Результат выполнения кода:
"application/x-www-form-urlencoded"
Смотрите также
-
MultipartFormDataContent,
форма с частями -
StringContent,
произвольный текст -
Headers,
заголовки тела -
HttpContent,
базовый класс