Restsharp 响应中缺少 Cookie,但在邮递员中没有



我能够使用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);

看起来您没有创建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);

最新更新