gpt4 book ai didi

c# - Restsharp 返回 403 而 Postman 返回 200

转载 作者:行者123 更新时间:2023-11-30 12:42:55 34 4
gpt4 key购买 nike

这是 Postman 为成功调用我的页面而提供的(修改后的)代码段。

var client = new RestClient("http://sub.example.com/wp-json/wp/v2/users/me");
var request = new RestRequest(Method.GET);
request.AddHeader("authorization", "Basic anVyYTp3MmZacmo2eGtBOHJsRWrt");
IRestResponse response = client.Execute(request);

但是当放置在我的 c# 应用程序中时,它返回 403 forbidden,而 Postman 成功并收到 200。当我在我的应用程序 (403) 中使用 httpclient 时,也会发生同样的事情。

最佳答案

使用RestClient.Authenticator相反:

var client = new RestClient("http://sub.example.com/wp-json/wp/v2/users/me")
{
Authenticator = new HttpBasicAuthenticator("User", "Pass")
};

var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);

编辑:

由于问题(如评论中所述)是 RestSharp 不通过重定向进行身份验证的事实,我建议结合使用 HttpClient 使用 HttpClientHandler,您可以在其中设置身份验证流程。

关于c# - Restsharp 返回 403 而 Postman 返回 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32299182/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com