Call works in Postman but not in C#(调用在 Postman 中有效,但在 C# 中无效)
问题描述
我正在尝试调用以下 URL,它在浏览器 (Chrome) 和 Postman 中都可以正常工作,但由于某种原因,它在 C# 中不起作用.
在浏览器中工作:
附:如果我删除 User-Agent 它仍然不起作用.为什么我在这里做错了?
感谢 Joshua &旺旺!
我将代码更改为以下代码后它正在工作:
var client = new RestClient("http://presta.craftingcrow.com/api/categories");var request = new RestRequest(Method.GET);request.AddHeader("缓存控制", "无缓存");request.AddHeader("授权", "基本" + Convert.ToBase64String(Encoding.Default.GetBytes("AJWKBLWT47VR26QWPNFCPJLXC6217F6F:"));IRestResponse 响应 = client.Execute(request);
无需添加用户代理或在主机名 (URL) 中包含密钥
I am trying to make a call to below URL and it works just fine in Browser (Chrome) and also in Postman, but for some reason, it doesn't work in C#.
Working in browser:
http://AJWKBLWT47VR26QWPNFCPJLXC6217F6F@presta.craftingcrow.com/api/categories
Working in Postman:
http://AJWKBLWT47VR26QWPNFCPJLXC6217F6F@presta.craftingcrow.com/api/categories
Not working in C# (RestSharp):
var client = new RestClient("http://AJWKBLWT47VR26QWPNFCPJLXC6217F6F@presta.craftingcrow.com/api/categories");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("User-Agent", @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
IRestResponse response = client.Execute(request);
Response:
401 Unauthorized
P.S. If I remove User-Agent it still doesn't work. Why am I doing wrong here?
Thanks to Joshua & Vhoang!
It's working after I changed code to below:
var client = new RestClient("http://presta.craftingcrow.com/api/categories");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("AJWKBLWT47VR26QWPNFCPJLXC6217F6F:"));
IRestResponse response = client.Execute(request);
There was no need to add user-agent or include key in the hostname (URL)
这篇关于调用在 Postman 中有效,但在 C# 中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:调用在 Postman 中有效,但在 C# 中无效


- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01