public Request(string method, string url, CookieJar cookies = null) { Method = method; Url = url; if (cookies != null) { CookieJar = cookies; } }
/// /// /// def morsel_to_cookie(morsel): /// """Convert a Morsel object into a Cookie containing the one k/v pair.""" /// /// expires = None /// if morsel['max-age']: /// try: /// expires = int(time.time() + int(morsel['max-age'])) /// except ValueError: /// raise TypeError('max-age: %s must be integer' % morsel['max-age']) /// elif morsel['expires']: /// time_template = '%a, %d-%b-%Y %H:%M:%S GMT' /// expires = calendar.timegm( /// time.strptime(morsel['expires'], time_template) /// ) /// return create_cookie( /// comment=morsel['comment'], /// comment_url=bool(morsel['comment']), /// discard=False, /// domain=morsel['domain'], /// expires=expires, /// name=morsel.key, /// path=morsel['path'], /// port=None, /// rest={'HttpOnly': morsel['httponly']}, /// rfc2109=False, /// secure=bool(morsel['secure']), /// value=morsel.value, /// version=morsel['version'] or 0, /// ) /// /// /// def cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True): /// """Returns a CookieJar from a key/value dictionary. /// /// :param cookie_dict: Dict of key/values to insert into CookieJar. /// :param cookiejar: (optional) A cookiejar to add the cookies to. /// :param overwrite: (optional) If False, will not replace cookies /// already in the jar with new ones. /// """ /// if cookiejar is None: /// cookiejar = RequestsCookieJar() /// /// if cookie_dict is not None: /// names_from_jar = [cookie.name for cookie in cookiejar] /// for name in cookie_dict: /// if overwrite or (name not in names_from_jar): /// cookiejar.set_cookie(create_cookie(name, cookie_dict[name])) /// /// return cookiejar public static void CookieJarFromDictionary(Dictionary<string, string> CookieDict, CookieJar CookieJar = null, bool Overwrite = true) { if (CookieJar == null) CookieJar = new RequestsCookieJar(); if (CookieDict != null) { var namesFromJar = CookieJar.Select(cookie => cookie.Name).ToList(); foreach (var name in CookieDict.Keys) { if (Overwrite || !(namesFromJar.Contains(name))) CookieJar.SetCookie() } } }
public Response(string domain, HttpResponseMessage response, CookieJar cookieJar) { CookieJar = cookieJar; StatusCode = (int)response.StatusCode; Data = response.Content.ReadAsByteArrayAsync().Complete(); response.Content.Headers.TryGetValues("Content-Type", out var ctval); if (ctval != null) { foreach (var ct in ctval) { ContentType = ct.Split(';', 2)[0]; if (ct.ToLower().Contains("charset=")) { switch (ct.Split("charset=", 2)[1].ToLower()) { case "utf-8": Encoding = Encoding.UTF8; break; case "iso-8859-1": Encoding = Encoding.ASCII; break; } } } } if (CookieJar != null) { response.Headers.TryGetValues("Set-Cookie", out var cval); if (cval != null) { foreach (var c in cval) { var v = c.Split(';', 2)[0].Split('=', 2); CookieJar.Set(domain, v[0], v[1]); } } } }
public Request AddCookieJar(CookieJar cookieJar) { CookieJar = cookieJar; return(this); }
public static Request Post(string url, CookieJar cookies = null) => new Request("POST", url, cookies);
public static Request Get(string url, CookieJar cookies = null) => new Request("GET", url, cookies);