从RestSharp切换到HttpClient

huangapple go评论63阅读模式
英文:

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.

  1. 使用在 RestClient 构造函数中的 HttpClient 实例,将在在启动 startup.cs 中配置的相应类中注入。
  2. HttpClient 中的 AddParameter 的替代方法是什么?因为这似乎不等同于在 HttpClient 中添加标头。

Any help on this appreciated.

英文:

Currently we are using RestClient from RestSharp library like below.

var client = new RestClient(&quot;&lt;some url&gt;&quot;);
var request = new RestRequest(&quot;&quot;, &quot;GET&quot;);
request.AddHeader(&quot;content-type&quot;, RestClientModel.ContentType);
request.AddParameter(&quot;redirect_uri&quot;, 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.

  1. Using HttpClient instance in RestClient constructor which will be injected in respective class which will configure in startup.cs.
  2. What is alternate for AddParameter in HttpClient? As this doesn't seems to be equivalent to adding headers in HttpClient.

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(&quot;https://search.me&quot;);
var request = new RestRequest(&quot;search&quot;)
    .AddParameter(&quot;foo&quot;, &quot;bar&quot;);
var response = await client.GetAsync&lt;SearchResponse&gt;(request);

> It will send a GET request to https://search.me/search?foo=bar&quot;.

You can try something like:

Dictionary&lt;string, string&gt; queryParams = new()
{
    { &quot;redirect_uri&quot;, RedirectUrl }
};
var query= await new FormUrlEncodedContent(queryParams).ReadAsStringAsync();

var response = await client.GetAsync($&quot;{someUrl}?{query}&quot;)

答案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


Dotnet Fiddle link

英文:

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(&quot;https://httpstat.us/&quot;) };
var client = new RestClient(httpClient);
var request = new RestRequest(&quot;200&quot;, Method.Get);
request.AddParameter(&quot;delay&quot;, &quot;1000&quot;);

Console.WriteLine(client.BuildUri(request).AbsoluteUri);	

This will print: https://httpstat.us/200?delay=1000


Dotnet Fiddle link

huangapple
  • 本文由 发表于 2023年5月26日 01:04:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定