c# - 相当于 C# (.NET) 中 Python 的 request.session.auth

标签 c# python restsharp

在Python中,我可以通过执行以下操作成功发出请求(通过授权):

def send_request(self, url, public_key, secret_key):
   session = requests.session()
   session.auth = (public_key, secret_key)
   return session.get(url)

我正在尝试在 C# 中复制此内容,但它未授权:

RestClient client = new RestClient(url);
RestRequest request = new RestRequest(url_stuff, Method.GET);
request.AddHeader(public_key, secret_key);
return client.Execute(request).Content;

我在这里缺少什么?

最佳答案

session.auth = (public_key, secret_key)

在 python 中是基本身份验证的简写,public_key 是用户名,secret_key 是密码。要对 RestClient 执行相同操作,您需要:

RestClient client = new RestClient(url);
client.Authenticator = new HttpBasicAuthenticator(public_key, secret_key);
return client.Execute(request).Content;

关于c# - 相当于 C# (.NET) 中 Python 的 request.session.auth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47490511/

相关文章:

c# - 程序集引用版本冲突——如何解决?

c# - 如何初始化尺寸在运行时决定的 C# 数组?

c# - 如何正确关闭客户端代理(现有连接被远程主机强行关闭)?

python - curl vs python "requests"访问 API 时

python - 什么是特征哈希(hashing-trick)?

python - jupyter notebook 无法识别 tensorflow

c# - 名为 'DefaultRoute' 的路由已经在路由集合中。路由名称必须是唯一的

c# - try catch 异步等待 https 异常

c# - 在 ServiceModel 客户端配置部分找不到引用契约(Contract)的默认端点元素......................

c# - 在 WPF 中获取数据网格中的多个选定行?