Restsharp响应中缺少Cookie,但Postman中没有

时间:2018-07-01 07:25:14

标签: c# postman restsharp

我可以使用Postman并使用Postman成功登录WebAPI并获取以下请求的Cookie。

我从Postman抓取代码,并使用RestSharp将其放入Visual Studio C#中。但是,使用Visual Studio和RestSharp时,请求可以成功返回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);

1 个答案:

答案 0 :(得分:0)

您似乎没有创建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);