将 HttpClient 转换为 RestSharp
Converting HttpClient to RestSharp
我有 Httpclient 函数,我正在尝试将其转换为 RestSharp,但我遇到了使用 google.
无法解决的问题
client.BaseAddress = new Uri("http://place.holder.nl/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",access_token);
HttpResponseMessage response = await client.GetAsync("api/personeel/myID");
string resultJson = response.Content.ReadAsStringAsync().Result;
此代码在我的 HttpClient 代码中,效果很好,但我无法在 RestSharp 中使用它,我在使用 RestSharp 时总是得到未授权:
RestClient client = new RestClient("http://place.holder.nl");
RestRequest request = new RestRequest();
client.Authenticator = new HttpBasicAuthenticator("Bearer", access_token);
request.AddHeader("Accept", "application/json");
request.Resource = "api/personeel/myID";
request.RequestFormat = DataFormat.Json;
var response = client.Execute(request);
我是否遗漏了身份验证的内容?
试试这个
RestClient client = new RestClient("http://place.holder.nl");
RestRequest request = new RestRequest("api/personeel/myID",Method.Get);
request.AddParameter("Authorization",$"Bearer {access_token}",ParameterType.HttpHeader);
request.AddHeader("Accept", "application/json");
request.RequestFormat = DataFormat.Json;
var response = client.Execute(request);
这解决了我的问题:
RestClient client = new RestClient("http://place.holder.nl");
RestRequest request = new RestRequest("api/personeel/myID", Method.GET);
request.AddParameter("Authorization",
string.Format("Bearer " + access_token),
ParameterType.HttpHeader);
var response = client.Execute(request);
在使用 Fiddler 嗅探后,我得出结论,RestSharp 将 access_token 作为 Basic 发送,因此使用普通参数而不是 HttpBasicAuthenticator 我可以强制使用 Bearer 前缀的令牌
如果有人遇到这种情况,从 V 106.6.10 开始,您可以简单地向客户端添加默认参数,从而不必向每个请求方法添加您的 Auth 令牌:
private void InitializeClient()
{
_client = new RestClient(BASE_URL);
_client.DefaultParameters.Add(new Parameter("Authorization",
string.Format("Bearer " + TOKEN),
ParameterType.HttpHeader));
}
我有 Httpclient 函数,我正在尝试将其转换为 RestSharp,但我遇到了使用 google.
无法解决的问题client.BaseAddress = new Uri("http://place.holder.nl/");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",access_token);
HttpResponseMessage response = await client.GetAsync("api/personeel/myID");
string resultJson = response.Content.ReadAsStringAsync().Result;
此代码在我的 HttpClient 代码中,效果很好,但我无法在 RestSharp 中使用它,我在使用 RestSharp 时总是得到未授权:
RestClient client = new RestClient("http://place.holder.nl");
RestRequest request = new RestRequest();
client.Authenticator = new HttpBasicAuthenticator("Bearer", access_token);
request.AddHeader("Accept", "application/json");
request.Resource = "api/personeel/myID";
request.RequestFormat = DataFormat.Json;
var response = client.Execute(request);
我是否遗漏了身份验证的内容?
试试这个
RestClient client = new RestClient("http://place.holder.nl");
RestRequest request = new RestRequest("api/personeel/myID",Method.Get);
request.AddParameter("Authorization",$"Bearer {access_token}",ParameterType.HttpHeader);
request.AddHeader("Accept", "application/json");
request.RequestFormat = DataFormat.Json;
var response = client.Execute(request);
这解决了我的问题:
RestClient client = new RestClient("http://place.holder.nl");
RestRequest request = new RestRequest("api/personeel/myID", Method.GET);
request.AddParameter("Authorization",
string.Format("Bearer " + access_token),
ParameterType.HttpHeader);
var response = client.Execute(request);
在使用 Fiddler 嗅探后,我得出结论,RestSharp 将 access_token 作为 Basic 发送,因此使用普通参数而不是 HttpBasicAuthenticator 我可以强制使用 Bearer 前缀的令牌
如果有人遇到这种情况,从 V 106.6.10 开始,您可以简单地向客户端添加默认参数,从而不必向每个请求方法添加您的 Auth 令牌:
private void InitializeClient()
{
_client = new RestClient(BASE_URL);
_client.DefaultParameters.Add(new Parameter("Authorization",
string.Format("Bearer " + TOKEN),
ParameterType.HttpHeader));
}