コード例 #1
0
 /// <summary>
 /// Gets the cookie or cookie chunks for the specified cookie name.
 /// </summary>
 public static Cookie[] GetCookieOrChunks(this CookieCollection cookies, string name)
 {
     return(cookies.Cast <Cookie>()
            .Where(x => x.Name == name ||
                   (x.Name.StartsWith(name + "C") && x.Name.TrimStart(name + "C").Is <int>()))
            .ToArray());
 }
コード例 #2
0
        public static SessionCookies FromCookieCollection(CookieCollection cookieCollection, Uri uri)
        {
            var cookies = cookieCollection.Cast <Cookie>();

            return(new SessionCookies(uri)
            {
                Cfduid = cookies.FirstOrDefault(c => c.Name.Equals(IdCookieName)),
                Cf_Clearance = cookies.FirstOrDefault(c => c.Name.Equals(ClearanceCookieName))
            });
        }
コード例 #3
0
        public static string GetCookieValue(CookieCollection collection, string cookieName)
        {
            Cookie cookie = collection.Cast <Cookie>().FirstOrDefault(n => n.Name.Equals(cookieName, StringComparison.OrdinalIgnoreCase));

            if (cookie == null)
            {
                return(string.Empty);
            }
            return(cookie.Value);
        }
コード例 #4
0
        /// <summary>
        /// generate cookie use by chromium
        /// </summary>
        /// <param name="cookie">cookie collection</param>
        /// <returns>cookie js string</returns>
        private CookieParam[] GenerateCookieParam(CookieCollection cookie)
        {
            var result = cookie.Cast <System.Net.Cookie>().Select(c => new CookieParam
            {
                Path   = c.Path,
                Name   = c.Name,
                Domain = c.Domain,
                Value  = c.Value
            }).ToArray();

            return(result);
        }
コード例 #5
0
        /// <summary>
        /// generate cookie use by PhantomJs
        /// </summary>
        /// <param name="cookie">cookie collection</param>
        /// <returns>cookie js string</returns>
        private string GenerateCookieJs(CookieCollection cookie)
        {
            var c = cookie.Cast <System.Net.Cookie>().Select(m => "phantom.addCookie(" + JsonConvert.SerializeObject(new
            {
                name   = m.Name,
                value  = m.Value,
                path   = m.Path,
                domain = m.Domain
            }) + ");").ToList();

            return(string.Join("\r\n\r\n", c));
        }
コード例 #6
0
        public static string ToCookieHeader(CookieCollection cookies)
        {
            if (cookies is null || cookies.Count == 0)
            {
                return(string.Empty);
            }

#if DotNetCore30Plus
            return(string.Join("; ", cookies.Select(t => t.Name + "=" + t.Value)));
#else
            return(string.Join("; ", cookies.Cast <Cookie>().Select(t => t.Name + "=" + t.Value)));
#endif
        }
コード例 #7
0
        /// <summary>
        /// Merges the specified cookies.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="cookies">The cookies.</param>
        public static void CookieMerge(this AutomaContext source, CookieCollection cookies)
        {
            var cookiesByName = cookies.Cast <Cookie>().ToDictionary(x => x.Name);
            var newCookies    = new CookieCollection {
                cookies
            };
            Cookie c;

            foreach (Cookie cookie in source.Cookies)
            {
                if (!cookiesByName.TryGetValue(cookie.Name, out c))
                {
                    newCookies.Add(cookie);
                }
            }
            source.Cookies = newCookies;
        }
コード例 #8
0
 /// <summary>
 /// 将cookie转为字符串
 /// </summary>
 /// <param name="cookies"></param>
 /// <returns></returns>
 public static string CookieTostr(CookieCollection cookies)
 {
     return(cookies.Cast <Cookie>()
            .Aggregate(string.Empty, (current, c) => current + (c.Name + "=" + c.Value + ";")));
 }
コード例 #9
0
ファイル: InstagramApi.cs プロジェクト: arash7388/PostBot
        public string PostData(string endpoint, string postData, string userAgent)
        {
            string url = $"{ApiEndpoint}{endpoint}";

            WebRequest           = (HttpWebRequest)System.Net.WebRequest.Create(url);
            WebRequest.UserAgent = userAgent;

            WebRequest.CookieContainer = new CookieContainer();
            //lets make sure we add our cookies back
            if (CookieCollection != null && CookieCollection.Count > 0)
            {
                WebRequest.CookieContainer.Add(CookieCollection);
            }
            WebRequest.Method      = "POST";
            WebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            try
            {
                var postBytes = Encoding.UTF8.GetBytes(postData);
                WebRequest.ContentLength = postBytes.Length;
                var postDataStream = WebRequest.GetRequestStream();
                postDataStream.Write(postBytes, 0, postBytes.Length);
                postDataStream.Close();
                try
                {
                    WebResponse = (HttpWebResponse)WebRequest.GetResponse();
                    //check if the status code is http 200 or http ok

                    if (WebResponse.StatusCode == HttpStatusCode.OK)
                    {
                        //get all the cookies from the current request and add them to the response object cookies

                        WebResponse.Cookies = WebRequest.CookieContainer.GetCookies(WebRequest.RequestUri);

                        if (WebResponse.Cookies.Count > 0)
                        {
                            //check if this is the first request/response, if this is the response of first request cookieCollection
                            //will be null
                            if (CookieCollection == null)
                            {
                                CookieCollection = WebResponse.Cookies;
                            }
                            else
                            {
                                foreach (Cookie oRespCookie in WebResponse.Cookies)
                                {
                                    var bMatch = false;
                                    foreach (
                                        var oReqCookie in
                                        CookieCollection.Cast <Cookie>()
                                        .Where(oReqCookie => oReqCookie.Name == oRespCookie.Name))
                                    {
                                        oReqCookie.Value = oRespCookie.Value;
                                        bMatch           = true;
                                        break;
                                    }
                                    if (!bMatch)
                                    {
                                        CookieCollection.Add(oRespCookie);
                                    }
                                }
                            }
                        }
                        var reader         = new StreamReader(WebResponse.GetResponseStream());
                        var responseString = reader.ReadToEnd();
                        reader.Close();
                        return(responseString);
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        using (var errorResponse = (HttpWebResponse)wex.Response)
                        {
                            using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                            {
                                var error = reader.ReadToEnd();
                                return(error);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return("Error in posting data");
        }
コード例 #10
0
 /// <summary>
 /// Get http header
 /// </summary>
 /// <param name="cookies">The cookie collection</param>
 /// <returns>To cookie header</returns>
 public static string GetHttpHeader(this CookieCollection cookies)
 {
     return(cookies.Cast <Cookie>().Select(c => string.Format("{0}={1};", c.Name, c.Value)).ToDelimitedString(";"));
 }
コード例 #11
0
 /// <summary>
 /// 按照名称查找Cookies,如果没有找到,则返回 null。
 /// </summary>
 /// <param name="container">当前的上下文</param>
 /// <param name="name">Cookies名称</param>
 /// <returns></returns>
 public static Cookie FindByName(this CookieCollection container, string name) => container?.Cast <Cookie>().FirstOrDefault(s => s.Name == name);
コード例 #12
0
 private IEnumerable <Cookie> AsCookiesEnumerable() =>
 Wrapped.Cast <Cookie>();
コード例 #13
0
ファイル: PixivClient.cs プロジェクト: moonheart/MyPixivUtils
 private string CookieToString(CookieCollection cookies)
 {
     return(string.Join("; ", cookies.Cast <Cookie>().Select(d => $"{d.Name}={d.Value}")));
 }