[英].NET HttpClient add query string and JSON body to POST
How do I set up a .NET HttpClient.SendAsync() request to contain query string parameters and a JSON body (in the case of a POST)? 如何设置.NET HttpClient.SendAsync()请求以包含查询字符串参数和JSON正文(在POST的情况下)?
// Query string parameters
var queryString = new Dictionary<string, string>()
{
{ "foo", "bar" }
};
// Create json for body
var content = new JObject(json);
// Create HttpClient
var client = new HttpClient();
client.BaseAddress = new Uri("https://api.baseaddress.com/");
var request = new HttpRequestMessage(HttpMethod.Post, "something");
// Setup header(s)
request.Headers.Add("Accept", "application/json");
// Add body content
request.Content = new StringContent(
content.ToString(),
Encoding.UTF8,
"application/json"
);
// How do I add the queryString?
// Send the request
client.SendAsync(request);
Every example I've seen says to set the 我见过的每个例子都说要设置
request.Content = new FormUrlEncodedContent(queryString)
but then I lose my JSON body initialization in the request.Content
但后来我在
request.Content
丢失了我的JSON主体初始化
I ended up finding Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString()
that was what I needed. 我最终找到了我需要的
Microsoft.AspNetCore.WebUtilities.QueryHelpers.AddQueryString()
。 This allowed me to add the query string parameters without having to build the string manually (and worry about escaping characters and such). 这允许我添加查询字符串参数,而无需手动构建字符串(并担心转义字符等)。
Note: I'm using ASP.NET Core, but the same method is also available through Microsoft.Owin.Infrastructure.WebUtilities.AddQueryString()
注意:我使用的是ASP.NET Core,但同样的方法也可以通过
Microsoft.Owin.Infrastructure.WebUtilities.AddQueryString()
New code: 新代码:
// Query string parameters
var queryString = new Dictionary<string, string>()
{
{ "foo", "bar" }
};
// Create json for body
var content = new JObject(json);
// Create HttpClient
var client = new HttpClient();
client.BaseAddress = new Uri("https://api.baseaddress.com/");
// This is the missing piece
var requestUri = QueryHelpers.AddQueryString("something", queryString);
var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
// Setup header(s)
request.Headers.Add("Accept", "application/json");
// Add body content
request.Content = new StringContent(
content.ToString(),
Encoding.UTF8,
"application/json"
);
// Send the request
client.SendAsync(request);
I can suggest that you use RestSharp for this purpose. 我建议您使用RestSharp来实现此目的。 It's basically a wrapper of the HttpWebRequest that does exactly what you want: makes it easy to compose url and body parameters and deserialize the result back.
它基本上是HttpWebRequest的包装器,可以完全按照您的要求执行操作:可以轻松编写url和body参数,并将结果反序列化。
Example from the site: 网站示例:
var client = new RestClient("http://example.com");
var request = new RestRequest("resource/{id}", Method.POST);
request.AddParameter("name", "value"); // adds to POST or URL querystring based on Method
request.AddUrlSegment("id", "123"); // replaces matching token in request.Resource
// easily add HTTP Headers
request.AddHeader("header", "value");
// add files to upload (works with compatible verbs)
request.AddFile(path);
// execute the request
IRestResponse response = client.Execute(request);
This is simple and works for me: 这很简单,对我有用:
responseMsg = await httpClient.PostAsJsonAsync(locationSearchUri, new { NameLike = "Johnson" });
The body of the requests look like { NameLike:"Johnson" }
请求的正文看起来像
{ NameLike:"Johnson" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.