【问题标题】:RESTSharp GET Call with Cookies带有 Cookie 的 RESTSharp GET 调用
【发布时间】:2021-04-21 07:01:26
【问题描述】:

使用 RESTSharp,我可以登录:

RestClient client = new RestClient(Constants.APIURL + "method/login");
        CookieContainer cookieJar = new CookieContainer();
        RestRequest request = new RestRequest(Method.POST);
        client.CookieContainer = cookieJar;
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Accept", "application/json");
        request.AddJsonBody(new
        {
            usr = username,
            pwd = password
        });

        var response = client.Execute(request);
        var cookie = HttpContext.Current.Server.UrlDecode(response.Headers.ToList().Find(x => x.Name == "Set-Cookie").Value.ToString());

然后我存储 cookie 并发送到另一个 API 调用,也通过 RESTSharp。

RestClient client = new RestClient(Constants.APIURL);
        RestRequest request = new RestRequest("resource/Asset", Method.GET);
        request.AddCookie("Cookie", HttpContext.Current.Server.UrlEncode(cookie));

但它一直返回 403 禁止。我在 POSTMan 上试过,效果很好。

有什么帮助吗?是我错误地发送了cookie吗?我尝试通过HttpWebRequest 发送 cookie,它工作得非常好。

【问题讨论】:

  • 您确定Cookie 是cookie 的正确名称吗?也许服务器期望cookie被称为authorization
  • 我从 POSTMAN 复制了 RESTSharp 生成的代码。

标签: c# postman restsharp rest


【解决方案1】:

我还尝试复制粘贴从 Postman 生成的代码,其中 cookie 在标头中传递,但它不起作用。我尝试如下发送 cookie 并且成功了

client.AddDefaultHeader("Cookie", cookie);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    相关资源
    最近更新 更多