[英]Calling Web API from Desktop Client
在WPF项目中工作。 发送的请求类型为“ application / x-www-form-urlencoded”的内容类型,非常适合Web客户端。 但是,当请求从WPF项目发送时,其内容从API接收为null。
public static string urlEncoded = "application/x-www-form-urlencoded";
public static async Task<HttpResponseMessage> SendRequest(HttpMethod method, string endPoint, dynamic content = null)
{
HttpResponseMessage response = null;
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(method, endPoint))
{
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(urlEncoded));
if (content != null)
{
string c;
if (content is string)
c = content;
else
c = JsonConvert.SerializeObject(content);
request.Content = new StringContent(c, Encoding.UTF8, urlEncoded);
}
response = await client.SendAsync(request).ConfigureAwait(false);
}
}
return response;
}
有人可以帮助我吗?
最终,我从中得到了解决方案。 只是更改内容
var keyValues = new List<KeyValuePair<string, string>>()
{
// Here Adding body
}
request.Content = new FormUrlEncodedContent(keyValues);
然后它的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.