【问题标题】:RestSharp Returning UnauthorizedRestSharp未经授权返回
【发布时间】:2015-09-04 19:16:53
【问题描述】:

我遇到了一个问题,我认为我可能在使用 RestSharp 时遗漏了一些东西。 我正在授权并取回一个 cookie 就好了……见下文。但是当我打电话获取数据时,它返回未经授权的数据。它在 Postman 中工作得很好,但在下面的代码中却不行。我正在使用控制台应用程序,我尝试通过 AddHeader、AddCookie 发送 cookie,并且只是作为参数。 responseLogin 确实包含正确的 cookie。任何帮助都会很棒。

    Dim clientLogin = New RestClient("http://[URI to Authorize]............")
    Dim requestLogin = New RestRequest(Method.POST)

    requestLogin.AddParameter("application/x-www-form-urlencoded", "[Username and password here.....]", ParameterType.RequestBody)
    Dim responseLogin As IRestResponse = clientLogin.Execute(requestLogin)




    Dim client = New RestClient("http://[URI to get data]............")
    Dim request = New RestRequest(Method.GET)

    request.AddHeader("Cookie", responseLogin.Cookies(0).Value.ToString)
    request.AddHeader("Accept", "application/json")

    Dim response As IRestResponse = client.Execute(request)

【问题讨论】:

    标签: vb.net restsharp


    【解决方案1】:

    Cookie 标头需要包含 cookie 的名称和值,例如

    Dim authCookie = responseLogin.Cookies(0) ' Probably should find by name
    request.AddHeader("Cookie", String.Format("{0}={1}", authCookie.Name, authCookie.Value))
    

    但是,the documentation(我个人从未使用过 RestSharp)表示 RestSharp 自动支持 cookie,因此如果您重用 RestClient 实例并设置 CookieContainer,则无需执行任何操作手动处理 cookie(除非您愿意,在某些情况下可能更可取)。

    Dim client = New RestClient(New Uri("[Base URI...]"))
    client.CookieContainer = New System.Net.CookieContainer()
    
    Dim requestLogin = New RestRequest("[login page path]", Method.POST)
    requestLogin.AddParameter("application/x-www-form-urlencoded", "[Username and password here.....]", ParameterType.RequestBody)
    Dim responseLogin As IRestResponse = client.Execute(requestLogin)
    
    Dim request = New RestRequest("[data api path", Method.GET)
    request.AddHeader("Accept", "application/json")
    Dim response As IRestResponse = client.Execute(request)
    

    您可能只是重复使用具有不同 RestClient 实例的 cookie 容器,而不是重复使用 client

    【讨论】:

      【解决方案2】:

      我在 RestClient .NET 框架 4.5.2 版本上遇到了同样的问题。 事实证明你必须实现 IAuthenticator 接口。

       public class MyAuth : IAuthenticator
      {
          readonly string _password;
          readonly string _passwordKey;
          readonly string _username;
          readonly string _usernameKey;
      
          public MyAuth(string usernameKey, string username, string passwordKey, string password)
          {
              _usernameKey = usernameKey;
              _username = username;
              _passwordKey = passwordKey;
              _password = password;
          }
      
          public void Authenticate(IRestClient client, IRestRequest request)
              => request
                  .AddCookie(_usernameKey, _username)
                  .AddCookie(_passwordKey, _password);
                  //.AddParameter(_usernameKey, _username)
                  //.AddParameter(_passwordKey, _password);
                  
      }
      

      我这样做了,我的请求成功了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 2018-11-05
        • 2016-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多