英文:
Moving from RestSharp to HttpClient
问题
Currently we are using RestClient
from RestSharp library like below.
var client = new RestClient("<some url>");
var request = new RestRequest("", "GET");
request.AddHeader("content-type", RestClientModel.ContentType);
request.AddParameter("redirect_uri", RedirectUrl);
RestResponse response = await client.ExecuteAsync(request);
return response;
We are facing performance issues while using RestClient
. That's the reason we want to move to HttpClient
. So, we need help on the following points.
- 使用在
RestClient
构造函数中的HttpClient
实例,将在在启动 startup.cs 中配置的相应类中注入。 HttpClient
中的AddParameter
的替代方法是什么?因为这似乎不等同于在HttpClient
中添加标头。
Any help on this appreciated.
英文:
Currently we are using RestClient
from RestSharp library like below.
var client = new RestClient("<some url>");
var request = new RestRequest("", "GET");
request.AddHeader("content-type", RestClientModel.ContentType);
request.AddParameter("redirect_uri", RedirectUrl);
RestResponse response = await client.ExecuteAsync(request);
return response;
We are facing performance issues while using RestClient
. That's the reason we want to move to HttpClient
. So, we need help on the following points.
- Using
HttpClient
instance inRestClient
constructor which will be injected in respective class which will configure in startup.cs. - What is alternate for
AddParameter
inHttpClient
? As this doesn't seems to be equivalent to adding headers inHttpClient
.
Any help on this appreciated.
答案1
得分: 1
GET
请求中,AddParameter
会添加查询字符串参数。来自文档的引用:
var client = new RestClient("https://search.me");
var request = new RestRequest("search")
.AddParameter("foo", "bar");
var response = await client.GetAsync<SearchResponse>(request);
它将发送一个GET请求到https://search.me/search?foo=bar"。
你可以尝试类似的操作:
Dictionary<string, string> queryParams = new()
{
{ "redirect_uri", RedirectUrl }
};
var query = await new FormUrlEncodedContent(queryParams).ReadAsStringAsync();
var response = await client.GetAsync($"{someUrl}?{query}")
英文:
For GET
request AddParameter
adds query string parameter. Quote from docs:
var client = new RestClient("https://search.me");
var request = new RestRequest("search")
.AddParameter("foo", "bar");
var response = await client.GetAsync<SearchResponse>(request);
> It will send a GET request to https://search.me/search?foo=bar".
You can try something like:
Dictionary<string, string> queryParams = new()
{
{ "redirect_uri", RedirectUrl }
};
var query= await new FormUrlEncodedContent(queryParams).ReadAsStringAsync();
var response = await client.GetAsync($"{someUrl}?{query}")
答案2
得分: 1
If you inject an HttpClient
to the RestClient
then your second question is pointless.
You can use the exact same code to add a parameter. You don't need to port that.
var httpClient = new HttpClient() { BaseAddress = new Uri("https://httpstat.us/") };
var client = new RestClient(httpClient);
var request = new RestRequest("200", Method.Get);
request.AddParameter("delay", "1000");
Console.WriteLine(client.BuildUri(request).AbsoluteUri);
This will print: https://httpstat.us/200?delay=1000
英文:
If you inject an HttpClient
to the RestClient
then your second question is pointless.
You can use the exact same code to add a parameter. You don't need to port that.
var httpClient = new HttpClient() { BaseAddress = new Uri("https://httpstat.us/") };
var client = new RestClient(httpClient);
var request = new RestRequest("200", Method.Get);
request.AddParameter("delay", "1000");
Console.WriteLine(client.BuildUri(request).AbsoluteUri);
This will print: https://httpstat.us/200?delay=1000
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论