private void WriteRequestCookies(HttpRequestMessage request) { if (!Cookies.Any()) { return; } var uri = request.RequestUri; var cookieHandler = HttpMessageHandler as HttpClientHandler; // if the inner handler is an HttpClientHandler (which it usually is), put the cookies in the CookieContainer. if (cookieHandler != null && cookieHandler.UseCookies) { if (cookieHandler.CookieContainer == null) { cookieHandler.CookieContainer = new CookieContainer(); } foreach (var cookie in Cookies.Values) { cookieHandler.CookieContainer.Add(uri, cookie); } } else { // http://stackoverflow.com/a/15588878/62600 request.Headers.TryAddWithoutValidation("Cookie", string.Join("; ", Cookies.Values)); } }
/// <summary> /// Clear all cookies in the browser /// </summary> /// <returns></returns> public ScreenshotJob ClearCookies() { if (Cookies.Any()) { Cookies = new ReadOnlyCollection <Cookie>(Enumerable.Empty <Cookie>().ToList()); } return(this); }
public override string ToString() { StringBuilder request = new StringBuilder(); request.Append($"{Method} {Url} {Constants.HttpOneProtocolFragment}"); request.Append(Environment.NewLine); if (Headers.Any()) request.Append(Headers.ToString()); if (Cookies.Any()) { request.Append(Constants.CookieRequestHeaderKey); request.Append($": {Cookies.ToString()}"); } request.Append(Environment.NewLine); return request.ToString(); }
public override string ToString() { StringBuilder request = new StringBuilder(); if (Headers.Any()) { request.Append(Headers.ToString() + Environment.NewLine); } if (Cookies.Any()) { request.Append(Constants.CookieRequestHeaderKey); request.Append($": {Cookies.ToString()}{Environment.NewLine}"); } return(request.ToString()); }
public override string ToString() { StringBuilder response = new StringBuilder(); response.Append(GlobalConstants.HttpOneProtocolFragment); response.Append($" {(int)StatusCode} {StatusCode}{Environment.NewLine}"); if (Headers.Any()) { response.Append(Headers.ToString() + Environment.NewLine); } if (Cookies.Any()) { response.Append(GlobalConstants.CookieResponseHeaderKey); response.Append($": {Cookies.ToString()}{Environment.NewLine}"); } if (Content.Length > 0) { response.Append(Environment.NewLine); } return(response.ToString()); }
public override string ToString() { StringBuilder response = new StringBuilder(); response.Append(Constants.HttpOneProtocolFragment); string statusText = Enumerator.ToTextOrDefault(StatusCode.GetType(), StatusCode); response.Append($" {statusText}"); response.Append(Environment.NewLine); if (Headers.Any()) { response.Append(Headers.ToString()); } if (Cookies.Any()) { response.Append(Constants.CookieResponseHeaderKey); response.Append($": {Cookies.ToString()}"); } response.Append(Environment.NewLine); return(response.ToString()); }
public static Cookie GetResponseCookie(HttpResponseMessage response) { if (response == null) { throw new ArgumentNullException("response", "The http response message to get the cookie from cannot be null."); } if (response.IsSuccessStatusCode) { IEnumerable <string> Cookies; /* * Cookie string * ibapauth="group=admin-group,ctime=1495590270,ip=192.168.1.101,auth=LOCAL,client=API,su=1,timeout=600,mtime=1495591213,user=admin,2CF4756i4DZ3lQ1rBn8t9epWMHYcWZ1TogQ"; httponly; Path=/; secure */ response.Headers.TryGetValues("Set-Cookie", out Cookies); if (Cookies != null && Cookies.Any()) { string CookieString = Cookies.FirstOrDefault(x => x.StartsWith("ibapauth")); if (!String.IsNullOrEmpty(CookieString)) { IEnumerable <string> CookieParts = CookieString.Split(';').Select(x => { return(x.Trim()); }); if (CookieParts.Any()) { if (!String.IsNullOrEmpty(CookieParts.First())) { Cookie Cookie = new Cookie("ibapauth", CookieParts.First().Replace("ibapauth=", ""), "", response.RequestMessage.RequestUri.Host); if (!String.IsNullOrEmpty(CookieParts.FirstOrDefault(x => x.Equals("secure", StringComparison.OrdinalIgnoreCase)))) { Cookie.Secure = true; } if (!String.IsNullOrEmpty(CookieParts.FirstOrDefault(x => x.Equals("httponly", StringComparison.OrdinalIgnoreCase)))) { Cookie.HttpOnly = true; } string[] ValueParts = CookieParts.First().Split(','); string TimeoutString = ValueParts.FirstOrDefault(x => x.StartsWith("timeout=", StringComparison.OrdinalIgnoreCase)); string ModifyTimeString = ValueParts.FirstOrDefault(x => x.StartsWith("mtime=", StringComparison.OrdinalIgnoreCase)); if (!String.IsNullOrEmpty(TimeoutString) && !String.IsNullOrEmpty(ModifyTimeString)) { try { string[] TimeoutParts = TimeoutString.Split('='); if (TimeoutParts.Length == 2) { int Timeout; if (Int32.TryParse(TimeoutParts[1], out Timeout)) { string[] ModifyTimeParts = ModifyTimeString.Split('='); if (ModifyTimeParts.Length == 2) { int Modify; if (Int32.TryParse(ModifyTimeParts[1], out Modify)) { Cookie.Expires = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Modify).AddSeconds(Timeout); } } } } } catch (Exception) { } } return(Cookie); } } } } } return(null); }