我想在登录后保存cookie,这对于以后的RestRequest来说很方便。
设置无效
client.CookieContainer = new CookieContainer ();
这是代码:
var _client = new RestClient("https://rocket-league.com");
_client.CookieContainer = new CookieContainer ();
var _request = new RestRequest(Method.GET);
_request.AddCookie("acceptedPrivacyPolicy", "2.0");
var _response = _client.Execute(_request);
// get csrf_token
// eg:<input type='hidden' name='csrf_token' value='2ea76eaca322e809c14ba608425f00f4' />
Regex _token = new Regex("<input type='hidden' name='csrf_token' value='(.*?)' />", RegexOptions.IgnoreCase);
Match match_token = _token.Match(_response.Content);
var csrf_token = "";
if (match_token.Success)
{
csrf_token = match_token.Groups[1].Value;
}
var client = new RestClient("https://rocket-league.com/functions/login.php");
var request = new RestRequest(Method.POST);
// **** get cookies *****
// ?????? who to do with here?????
request.AddCookie("acceptedPrivacyPolicy", "2.0");
// 5. adds to POST or URL querystring based on Method
request.AddParameter("email", email);
request.AddParameter("password", password);
request.AddParameter("submit", "Go");
request.AddParameter("rememberme", "on");
request.AddParameter("csrf_token", csrf_token);
IRestResponse response = client.Execute(request);