Instantly share code, notes, and snippets.
Last active
April 5, 2016 23:39
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save RyuaNerin/a03b5f6ee6866d571ebd9714c1043b32 to your computer and use it in GitHub Desktop.
Convert Set-Cookie header of WebResponse to CookieCollection https://ryuanerin.kr/post/2016-03-31-parse-cookie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MyCookieContainer.Add(StringToCookies(res.Headers["Set-Cookie"], uri.Host)); | |
| /////////////////////////// | |
| private static CookieCollection StringToCookies(string setCookies, string domain) | |
| { | |
| var cc = new CookieCollection(); | |
| int i, k; | |
| string[] kvs; | |
| string key, val; | |
| foreach (var str in SplitEachCookie(setCookies)) | |
| { | |
| var cookie = new Cookie(); | |
| cookie.Domain = domain; | |
| cookie.Path = "/"; | |
| kvs = str.Split(';'); | |
| for (i = 0; i < kvs.Length; ++i) | |
| { | |
| k = kvs[i].IndexOf('='); | |
| if (k == -1) | |
| { | |
| key = kvs[i]; | |
| val = null; | |
| } | |
| else | |
| { | |
| key = kvs[i].Substring(0, k); | |
| val = kvs[i].Substring(k + 1); | |
| } | |
| if (i == 0) | |
| { | |
| cookie.Name = key; | |
| cookie.Value = val; | |
| } | |
| else | |
| { | |
| key = key.ToLower(); | |
| if (string.IsNullOrWhiteSpace(val)) | |
| { | |
| switch (key) | |
| { | |
| case "httponly": | |
| cookie.HttpOnly = true; | |
| break; | |
| case "discard": | |
| cookie.Discard = true; | |
| break; | |
| case "secure": | |
| cookie.Secure = true; | |
| break; | |
| } | |
| } | |
| else | |
| { | |
| switch (key) | |
| { | |
| case "expires": | |
| if (cookie.Expires == DateTime.MinValue) | |
| cookie.Expires = ParseCookieExpires(val); | |
| break; | |
| case "max-age": | |
| if (cookie.Expires == DateTime.MinValue) | |
| cookie.Expires = cookie.TimeStamp.AddSeconds(int.Parse(val)); | |
| break; | |
| case "path": | |
| cookie.Path = val; | |
| break; | |
| case "domain": | |
| cookie.Domain = val; | |
| break; | |
| case "port": | |
| cookie.Port = val; | |
| break; | |
| case "comment": | |
| cookie.Comment = val; | |
| break; | |
| case "commenturl": | |
| try | |
| { | |
| cookie.CommentUri = new Uri(val); | |
| } | |
| catch { } | |
| break; | |
| case "version": | |
| try | |
| { | |
| cookie.Version = int.Parse(val); | |
| } | |
| catch { } | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| cc.Add(cookie); | |
| } | |
| return cc; | |
| } | |
| private readonly static string[] cookieExpiresFormats = | |
| { | |
| "r", | |
| "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'GMT'", | |
| "ddd, dd'-'MMM'-'yy HH':'mm':'ss 'GMT'", | |
| "ddd, dd'-'MMM'-'yyyy HH':'mm':'ss 'UTC'", | |
| "ddd, dd'-'MMM'-'yy HH':'mm':'ss 'UTC'" | |
| }; | |
| private static IEnumerable<string> SplitEachCookie(string setCookies) | |
| { | |
| var lst = new List<string>(); | |
| int read = 0; | |
| int find = 0; | |
| while (read < setCookies.Length) | |
| { | |
| find = setCookies.IndexOf(',', read + 1); | |
| if (find != -1 && setCookies.IndexOf("expires=", read, find - read, StringComparison.CurrentCultureIgnoreCase) >= 0) | |
| find = setCookies.IndexOf(',', find + 1); | |
| if (find == -1) | |
| find = setCookies.Length; | |
| yield return setCookies.Substring(read, find - read); | |
| read = find + 1; | |
| } | |
| } | |
| private static DateTime ParseCookieExpires(string value) | |
| { | |
| if (String.IsNullOrWhiteSpace(value)) | |
| return DateTime.MinValue; | |
| DateTime date; | |
| for (int i = 0; i < cookieExpiresFormats.Length; i++) | |
| { | |
| if (DateTime.TryParseExact(value, cookieExpiresFormats[i], CultureInfo.CurrentCulture, DateTimeStyles.None, out date)) | |
| { | |
| date = DateTime.SpecifyKind(date, DateTimeKind.Utc); | |
| return TimeZone.CurrentTimeZone.ToLocalTime(date); | |
| } | |
| } | |
| return DateTime.MinValue; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment