代码之家  ›  专栏  ›  技术社区  ›  Alex McLean catherine

相当于Python的请求。一场C#(.NET)中的身份验证

  •  2
  • Alex McLean catherine  · 技术社区  · 7 年前

    在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;
    

    我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  3
  •   Evk    7 年前
    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;