我的内容是:
var content = new Dictionary<string, string>
{
{"pickup_date", pickupDate.ToString("dd.MM.yyyy HH:mm")},
{"to_city", "Victoria"},
{"delivery_company", "4"},
{"shop_refnum", parameters.Reference},
{"dimension_side1", "20"},
{"dimension_side2", "20"},
{"dimension_side3", "20"},
{"weight", "5"}
};
var httpContent = new FormUrlEncodedContent(content);
如何从httpContent中提取内容?
答案 0 :(得分:0)
在我编写的某些单元测试中,我不得不从请求中提取数据信息。像其他答案一样,可以在System.Net.Http
名称空间中使用以下方法(我使用的是.NET Core 3.1):
public static Task<NameValueCollection> ReadAsFormDataAsync(this HttpContent content);
应用于HttpContent
实例:
var content = request.Content.ReadAsFormDataAsync();
var username = content.Result["username"];
答案 1 :(得分:-2)
我在System.Net.Http.Formatting中找到了ReadAsFormDataAsync()方法:
NameValueCollection hcData = await httpContent.ReadAsFormDataAsync(cancellationToken);