public void GetAllCookies_Empty_ReturnsEmptyCollection() { var container = new CookieContainer(); Assert.NotNull(container.GetAllCookies()); Assert.NotSame(container.GetAllCookies(), container.GetAllCookies()); Assert.Empty(container.GetAllCookies()); }
/// <summary> /// Sends a POST request towards UniFi /// </summary> /// <param name="url">Url to POST the postData to</param> /// <param name="postData">Data to send to the UniFi service, typically a JSON payload</param> /// <param name="cookieContainer">Cookies which have been recorded for this session</param> /// <param name="timeout">Timeout in milliseconds on how long the request may take. Default = 60000 = 60 seconds.</param> /// <returns>The website contents returned by the webserver after posting the data</returns> public async static Task <string> PostRequest(Uri url, string postData, CookieContainer cookieContainer, int timeout = 60000) { // Construct the POST request var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json, text/plain, */*"; request.ContentType = "application/json;charset=UTF-8"; request.CookieContainer = cookieContainer; request.Timeout = timeout; request.KeepAlive = false; // Check if the have a Cross Site Request Forgery cookie and if so, add it as the X-Csrf-Token header which is required by UniFi when making a POST var csrfCookie = cookieContainer.GetAllCookies().FirstOrDefault(c => c.Name == "csrf_token"); if (csrfCookie != null) { request.Headers.Add("X-Csrf-Token", csrfCookie.Value); } // Convert the POST data to a byte array var postDataByteArray = Encoding.UTF8.GetBytes(postData); // Set the ContentLength property of the WebRequest. request.ContentLength = postDataByteArray.Length; // Get the request stream using (var postDataStream = await request.GetRequestStreamAsync()) { // Write the POST data to the request stream await postDataStream.WriteAsync(postDataByteArray, 0, postDataByteArray.Length); // Close the Stream object postDataStream.Close(); } // Receive the response from the webserver using (var response = await request.GetResponseAsync() as HttpWebResponse) { // Make sure the webserver has sent a response if (response == null) { return(null); } using (var requestDataStream = response.GetResponseStream()) { // Make sure the datastream with the response is available if (requestDataStream == null) { return(null); } using (var reader = new StreamReader(requestDataStream)) { return(await reader.ReadToEndAsync()); } } } }
private void HandleResponseCookies(HttpResponse response, CookieContainer container) { foreach (Cookie cookie in container.GetAllCookies()) { cookie.Expired = true; } var cookieHeaders = response.GetCookieHeaders(); if (cookieHeaders.Empty()) { return; } AddCookiesToContainer(response.Request.Url, cookieHeaders, container); if (response.Request.StoreResponseCookie) { lock (_cookieContainerCache) { var persistentCookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer()); AddCookiesToContainer(response.Request.Url, cookieHeaders, persistentCookieContainer); } } }
public QQHttpCookie GetCookie(string name, string url) { var list = _cookieContainer.GetAllCookies().ToList(); QQHttpCookie qqHttpCookie = null; Cookie cookie = _cookieContainer.GetCookies(new Uri(url))[name] ?? _cookieContainer.GetCookies(name).FirstOrDefault(); if (cookie != null) { qqHttpCookie = new QQHttpCookie(cookie); } else { MyLogger.Default.Error($"获取cookie失败:{name}"); } return(qqHttpCookie); }
public static void CopyCookieContainer(this WebSession session, CookieContainer container) { foreach (Cookie c in container.GetAllCookies()) { session.SetCookie(new Uri(c.Domain), String.Format("{0}={1};", c.Name, c.Value), c.HttpOnly, true); // (yn) Crossing Fingers } }
public static string GetCookie(this CookieContainer cookieJar, string key) { foreach (Cookie co in cookieJar.GetAllCookies()) { if (co.Name == key) { return(co.Value); } } return(""); }
private static HttpClient GetClient(CookieContainer cookieContainer, WebProxy webProxy) { if (webProxy != null) { _webProxy.Address = webProxy.Address; } if (cookieContainer != null) { _cookieContainer.GetAllCookies().Select(s => s.Domain).Distinct().ToList().ForEach(f => { foreach (var cookie in _cookieContainer.GetCookies(new Uri("http://www" + f)).OfType <Cookie>()) { cookie.Expired = false; } }); cookieContainer.GetAllCookies().ForEach(_cookieContainer.Add); } if (_client == null) { lock (locker) { if (_client == null) { _client = new HttpClient(new HttpClientHandler { CookieContainer = _cookieContainer ?? new CookieContainer(), Proxy = _webProxy, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.None }); _client.DefaultRequestHeaders.Connection.Add("keep-alive"); } } } return(_client); }
public void Test_Login() { var cookieContainer = new CookieContainer(); var client = new CommonFunctionalityClient(cookieContainer, SITE_URL); var task = client.LoginPost(PASSWORD, USERNAME); task.Wait(); var httpClient = task.Result; Assert.IsTrue(httpClient.Response.StatusCode == HttpStatusCode.OK); var responseCookies = cookieContainer.GetAllCookies(); var authCookie = responseCookies["atoken"]; Assert.IsNotNull(authCookie); }
/// <summary> /// 使用 JS 引擎请求页面 /// </summary> /// <param name="url"></param> /// <param name="cookieContainer"></param> /// <returns></returns> public string QueryRequest(string url, CookieContainer cookieContainer = null) { var domain = Regex.Matches(url, "(?i)(https*?://)*(.+?)/")[0].Result("$2"); var index = domain.IndexOf(".", StringComparison.Ordinal); if (index != domain.LastIndexOf(".", StringComparison.Ordinal)) { domain = domain.Substring(index); } cookieContainer.GetAllCookies().ForEach(f => { _driver.Manage().Cookies.AddCookie(new OS.Cookie(f.Name, f.Value, domain, "/", DateTime.Now.AddDays(1))); }); _driver.Url = url; return(_driver.FindElementByTagName("body").Text); }
public async Task <List <Cookie> > Authenticate() { var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri("https://users.premierleague.com/accounts/login/", UriKind.Absolute), Content = new FormUrlEncodedContent(new Dictionary <string, string> { ["login"] = _options.Login, ["password"] = _options.Password, ["app"] = "plfpl-web", ["redirect_uri"] = "https://fantasy.premierleague.com/" }), Headers = { { "Origin", "https://fantasy.premierleague.com" }, { "Referer", "https://fantasy.premierleague.com" } } }; var cookieJar = new CookieContainer(); var handler = new HttpClientHandler { CookieContainer = cookieJar, UseCookies = true, UseDefaultCredentials = false }; var httpClient = new HttpClient(handler); var response = await httpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { throw new FplApiException($"Could not authenticate! Status code : {response.StatusCode}"); } var usersCookies = cookieJar.GetAllCookies(); VerifyAuthCookies(usersCookies, "sessionid", "pl_profile", "csrftoken"); return(usersCookies); }
public static List <Market> GetMarketsByEvent(long id, WebProxy proxy, CookieContainer cont, string host) { using (var wc = new PostWebClient(proxy, cont.GetAllCookies())) { var query = $"{{\"jsonrpc\":\"2.0\",\"method\":\"frontend/market/get\",\"id\":{new Random().Next(100, 9999)},\"params\":{{\"by\":{{\"lang\":\"en\",\"service_id\":1,\"event_id\":{id}}}}}}}"; var response = wc.UploadString($"{host}frontend_api2/", query); var t = JsonConvert.DeserializeObject <FrontendApiResponseWrapper <Market> >(response); //foreach (var market in t.Result) //{ // foreach (var outcome in market.outcomes) // { // ProxyHelper.UpdateFavbetEvents(market.market_name + " | " + market.result_type_name + " | " + outcome.outcome_name + " | " +outcome.outcome_param); // } //} return(t.Result); } }
public void GetAllCookies_NonEmpty_AllCookiesReturned() { var container = new CookieContainer(); container.PerDomainCapacity = 100; container.Capacity = 100; Cookie[] cookies = Enumerable.Range(0, 100).Select(i => new Cookie($"name{i}", $"value{i}")).ToArray(); Uri[] uris = new[] { new Uri("https://dot.net"), new Uri("https://source.dot.net"), new Uri("https://microsoft.com") }; for (int i = 0; i < cookies.Length; i++) { container.Add(uris[i % uris.Length], cookies[i]); } CookieCollection actual = container.GetAllCookies(); Assert.Equal(cookies.Length, actual.Count); Assert.Equal( cookies.Select(c => c.Name + "=" + c.Value).ToHashSet(), actual.Select(c => c.Name + "=" + c.Value).ToHashSet()); }
/// <summary> /// HttpClient 请求 /// </summary> /// <param name="url"></param> /// <param name="method"></param> /// <param name="dictionary"></param> /// <param name="cookieContainer"></param> /// <param name="referer"></param> /// <param name="contentType"></param> /// <param name="accept"></param> /// <param name="userAgent"></param> /// <param name="isRandomIP"></param> /// <param name="webProxy"></param> /// <returns></returns> private static DataResult <HttpContent> Request(string url, HttpMethod method, IEnumerable <KeyValuePair <string, string> > dictionary = null, CookieContainer cookieContainer = null, string referer = "", string contentType = "", string accept = "", string userAgent = "", bool isRandomIP = false, IWebProxy webProxy = null) { var dataResult = new DataResult <HttpContent>(); if (url.Contains("https")) { ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true; } try { using (var client = new HttpClient(new HttpClientHandler { CookieContainer = cookieContainer ?? new CookieContainer(), Proxy = webProxy, AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate | DecompressionMethods.None })) { client.DefaultRequestHeaders.Add("Accept", string.IsNullOrWhiteSpace(accept) ? defaultAccept : accept); client.DefaultRequestHeaders.Add("User-Agent", string.IsNullOrWhiteSpace(userAgent) ? defaultUserAgent : userAgent); //if (isRandomIP) client.DefaultRequestHeaders.Add("X-Forwarded-For", BaseFanctory.GetRandomHost()); if (!string.IsNullOrEmpty(referer)) { client.DefaultRequestHeaders.Add("Referer", referer); } var httpRequestMessage = new HttpRequestMessage(method, new Uri(url)); if (method == HttpMethod.Post && dictionary != null) { var httpContent = new FormUrlEncodedContent(dictionary); httpContent.Headers.ContentType = new MediaTypeHeaderValue(string.IsNullOrEmpty(contentType) ? defaultContentType : contentType); httpRequestMessage.Content = httpContent; } dataResult.Data = client.SendAsync(httpRequestMessage).Result.Content; return(dataResult); } } catch (WebException ex) { dataResult.IsSuccess = false; dataResult.ErrorMsg = $"Web响应异常,请求Url:{url},{JsonConvert.SerializeObject(dictionary)} 异常信息:{ex.Message}"; return(dataResult); } catch (Exception ex) { while (true) { if (ex.InnerException == null) { break; } ex = ex.InnerException; } LogFactory.Error($"请求异常!请求Url:{url} 请求参数:{JsonConvert.SerializeObject(dictionary)} {Environment.NewLine}异常信息:{ex.Message}{Environment.NewLine}{ex.StackTrace}"); dataResult.IsSuccess = false; dataResult.ErrorMsg = $"请求异常!请求Url:{url} 异常信息:{ex.Message} 详情请错误看日志!"; return(dataResult); } finally { if (cookieContainer != null) { cookieContainer.GetAllCookies().Select(s => s.Domain).Distinct().ToList().ForEach(f => { foreach (var cookie in cookieContainer.GetCookies(new Uri("http://www" + f)).OfType <Cookie>()) { cookie.Expired = false; } }); _cookieContainer.GetAllCookies().ForEach(cookieContainer.Add); } } }
public IReadOnlyList <Cookie> GetAllCookies() { return(UseCookie ? (IReadOnlyList <Cookie>)_cookieContainer.GetAllCookies() : Array.Empty <Cookie>()); }