Equivalent of Python's request.session.auth in C# (.NET)

我的未来我决定 提交于 2019-12-13 15:58:50

问题


In Python I can successfully make a request (with authorization passing) by doing:

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

I'm trying to replicate this in C# but it's not authorizing:

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

What am I missing here?


回答1:


session.auth = (public_key, secret_key)

in python is shorthand for Basic authentication, with public_key being user name and secret_key a password. To do the same with RestClient you need to:

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


来源:https://stackoverflow.com/questions/47490511/equivalent-of-pythons-request-session-auth-in-c-sharp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!