【问题标题】:Cookie missing in Restsharp response but not in PostmanRestsharp 响应中缺少 Cookie,但 Postman 中没有
【发布时间】:2018-12-09 19:12:48
【问题描述】:

我能够使用 Postman 并使用 Postman 通过 WebAPI 成功登录,并为以下请求获取 cookie。

我从 Postman 获取代码并使用 RestSharp 放入 Visual Studio C#。但是使用 Visual Studio 和 RestSharp,请求返回成功 ok 200 但没有 COOKIE。

我错过了什么?我已经在网上找了 4 天了。

感谢您的帮助。下面是 Postman 的 C# 代码:

var client = new RestClient("https://client.awebsite.ca/user/login?_format=hal_json");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "a16887c6-a1da-fa25-e721-621c4b19318b");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "text/plain");
request.AddParameter("undefined", "{\"name\":\"firstname.lastname\", \"pass\":\"passwordoffirstnamelastname\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

【问题讨论】:

  • 为什么你知道没有cookie?
  • 嗨 Badulake,我查看了 reponse.cookies,计数为 0。但是,按照 Ben Hall 的建议将 cookie 容器添加到客户端后,我在执行后更深入地查看了该 cookie 容器对象请求并可以找到一个 cookie 并将其用于我以后的任务。谢谢本

标签: c# postman restsharp


【解决方案1】:

您似乎没有创建CookieContainer

见:https://github.com/restsharp/RestSharp/wiki/Cookies

var client = new RestClient("https://client.awebsite.ca/user/login?_format=hal_json");
client.CookieContainer = new System.Net.CookieContainer();

// Your request code...

IRestResponse response = client.Execute(request);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    相关资源
    最近更新 更多