Пример #1
0
            public static void SendHeader(string sHttpVersion, string sMIMEHeader, long iTotBytes, string sStatusCode, ref TcpClient mySocket)
            {
                String sBuffer = "";

                if (sMIMEHeader.Length == 0)
                {
                    sMIMEHeader = "text/html"; // 默认 text/html
                }
                sBuffer = sBuffer + sHttpVersion + sStatusCode + "\r\n";
                sBuffer = sBuffer + "Author: Rixiang Yu \r\n";
                sBuffer = sBuffer + "Server: UcAsp.Net \r\n";
                sBuffer = sBuffer + "Content-Type: " + sMIMEHeader + "\r\n";
                sBuffer = sBuffer + "Accept-Ranges: bytes\r\n";
                sBuffer = sBuffer + "Accept-Encoding: gzip,deflate\r\n";
                if (!Cookies.ContainsKey("UcAsp.Net_SessionId"))
                {
                    Session   = new HttpSession();
                    SessionId = Session.SessionId;
                    sBuffer   = sBuffer + "Set-Cookie: UcAsp.Net_SessionId=" + Session.SessionId + "\r\n";
                }
                else
                {
                    SessionId = Cookies["UcAsp.Net_SessionId"];
                }
                if (iTotBytes != 0)
                {
                    sBuffer = sBuffer + "Content-Length: " + iTotBytes + "\r\n\r\n";
                }
                SendToBrowser(sBuffer, ref mySocket);
            }
Пример #2
0
 public bool ContainsCookie(string name)
 {
     if (Cookies == null)
     {
         return(false);
     }
     return(Cookies.ContainsKey(name));
 }
Пример #3
0
 public bool Contains(string cookieName)
 {
     if (Cookies.ContainsKey(cookieName))
     {
         return(true);
     }
     return(false);
 }
        private void _SetSession()
        {
            if (Cookies.ContainsKey(SessionStore.SessionCookieKey))
            {
                var cookie    = Cookies.Get(SessionStore.SessionCookieKey);
                var sessionId = cookie.Value;

                Session = SessionStore.GetOrAdd(id: sessionId);
            }
        }
Пример #5
0
 public void AddCookie(string name, string value)
 {
     if (!Cookies.ContainsKey(name))
     {
         Cookies.Add(name, value);
     }
     else
     {
         Cookies[name] = value;
     }
 }
Пример #6
0
 /// <summary>
 /// 删除Cookie
 /// </summary>
 /// <param name="key">键名</param>
 public void DelCookie(string key)
 {
     if (Cookies.ContainsKey(key))
     {
         Cookies[key].Value = null;
     }
     else
     {
         Cookies.Add(key, new CookieModel()
         {
             Value = null
         });
     }
 }
Пример #7
0
 public static void GetCookie(this HttpResponseHeaders headers)
 {
     foreach (var cookie in headers.ToImmutableDictionary()["Set-Cookie"])
     {
         var pair  = cookie.Split(';')[0].Split('=');
         var key   = pair[0];
         var value = pair[1].Replace("\"", "");
         if (!Cookies.ContainsKey(key))
         {
             Cookies.Add(key, value);
         }
     }
     if (Cookies.ContainsKey("auth_key"))
     {
         Uid = Cookies["auth_key"];
     }
 }
Пример #8
0
 /// <summary>
 /// 设置cookie
 /// </summary>
 /// <param name="key">键名</param>
 /// <param name="value">键值</param>
 /// <param name="expires">有效期,默认为0不限期</param>
 public void SetCookie(string key, string value, int expires = 0)
 {
     if (Cookies.ContainsKey(key))
     {
         if (expires > 0)
         {
             if (Cookies[key].Options != null)
             {
                 Cookies[key].Value           = value;
                 Cookies[key].Options.Expires = expires;
             }
             else
             {
                 Cookies.Add(key, new CookieModel()
                 {
                     Value = value, Options = new CookieOptionsModel()
                     {
                         Expires = expires
                     }
                 });
             }
         }
         else
         {
             Cookies[key].Value = value;
         }
     }
     else if (expires > 0)
     {
         Cookies.Add(key, new CookieModel()
         {
             Value = value, Options = new CookieOptionsModel()
             {
                 Expires = expires
             }
         });
     }
     else
     {
         Cookies.Add(key, new CookieModel()
         {
             Value = value
         });
     }
 }
Пример #9
0
        public void WriteArguments(string flag, StringBuilder builder)
        {
            if (UserName.HasValue())
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, " --username {0}", UserName);
            }

            if (Password.HasValue())
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, " --password {0}", Password);
            }

            if (Post != null && Post.Count > 0)
            {
                CreateRepeatableFlags("--post", Post, builder);
            }

            if (Cookies != null && Cookies.Count > 0)
            {
                CreateRepeatableFlags("--cookie", Cookies, builder);
            }

            // Send FormsAuthentication Cookie
            var ctx = HttpContext.Current;

            if (SendAuthCookie && ctx != null && ctx.Request != null && ctx.Request.Cookies != null)
            {
                var authCookieName = FormsAuthentication.FormsCookieName;
                if (Cookies == null || !Cookies.ContainsKey(authCookieName))
                {
                    HttpCookie authCookie = null;
                    if (ctx.Request.Cookies.AllKeys.Contains(authCookieName))
                    {
                        authCookie = ctx.Request.Cookies[authCookieName];
                    }
                    if (authCookie != null)
                    {
                        var authCookieValue = authCookie.Value;
                        builder.AppendFormat(CultureInfo.InvariantCulture, " {0} {1} {2}", "--cookie", authCookieName, authCookieValue);
                    }
                }
            }
        }
Пример #10
0
        public void AddOrUpdateCookie(string key, string value)
        {
            if (Cookies == null)
            {
                Cookies = new Dictionary <string, string>();
            }

            if (Cookies.ContainsKey(key))
            {
                if (Cookies[key] != value)
                {
                    Cookies[key]      = value;
                    _allCookiesString = GenerateCookieString();
                }
            }
            else
            {
                Cookies.Add(key, value);
                _allCookiesString = GenerateCookieString();
            }
        }
Пример #11
0
        public WebPage RequestPage(Uri url, string content, string method, string contentType)
        {
            string          htmlResult;
            HttpWebRequest  request  = (HttpWebRequest)HttpWebRequest.Create(url);
            HttpWebResponse response = null;
            ASCIIEncoding   encoding = new ASCIIEncoding();

            byte[] contentData = encoding.GetBytes(content);

            request.Proxy             = Proxy;
            request.Timeout           = 60000;
            request.Method            = method;
            request.AllowAutoRedirect = false;
            request.Accept            = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.Referer           = LastUrl;
            request.KeepAlive         = false;

            request.UserAgent = UserAgent;

            request.Headers.Add("Accept-Language", "en-us,en;q=0.5");
            //request.Headers.Add("UA-CPU", "x86");
            request.Headers.Add("Cache-Control", "no-cache");
            request.Headers.Add("Accept-Encoding", "gzip,deflate");

            String cookieString = "";

            foreach (KeyValuePair <String, String> cookiePair in Cookies)
            {
                cookieString += cookiePair.Key + "=" + cookiePair.Value + ";";
            }

            if (cookieString.Length > 2)
            {
                String cookie = cookieString.Substring(0, cookieString.Length - 1);
                request.Headers.Add("Cookie", cookie);
            }

            if (method == "POST")
            {
                request.ContentLength = contentData.Length;
                request.ContentType   = contentType;

                Stream contentWriter = request.GetRequestStream();
                contentWriter.Write(contentData, 0, contentData.Length);
                contentWriter.Close();
            }

            int attempts = 0;

            while (true)
            {
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                    if (response == null)
                    {
                        throw new WebException();
                    }

                    break;
                }
                catch (WebException)
                {
                    if (response != null)
                    {
                        response.Close();
                    }

                    if (attempts == PageReattempts)
                    {
                        throw;
                    }

                    // Wait three seconds before trying again
                    Thread.Sleep(3000);
                }

                attempts += 1;
            }

            // Tokenize cookies
            if (response.Headers["Set-Cookie"] != null)
            {
                String headers = response.Headers["Set-Cookie"].Replace("path=/,", ";").Replace("HttpOnly,", "");
                foreach (String cookie in headers.Split(';'))
                {
                    if (cookie.Contains("="))
                    {
                        String[] splitCookie = cookie.Split('=');
                        String   cookieKey   = splitCookie[0].Trim();
                        String   cookieValue = splitCookie[1].Trim();

                        if (Cookies.ContainsKey(cookieKey))
                        {
                            Cookies[cookieKey] = cookieValue;
                        }
                        else
                        {
                            Cookies.Add(cookieKey, cookieValue);
                        }
                    }
                    else
                    {
                        if (Cookies.ContainsKey(cookie))
                        {
                            Cookies[cookie] = "";
                        }
                        else
                        {
                            Cookies.Add(cookie, "");
                        }
                    }
                }
            }

            htmlResult = ReadResponseStream(response);
            response.Close();

            if (response.Headers["Location"] != null)
            {
                response.Close();
                Thread.Sleep(1500);
                String  newLocation = response.Headers["Location"];
                WebPage result      = RequestPage(newLocation);
                return(new WebPage(result.Html, new WebPage(htmlResult)));
            }

            LastUrl = url.ToString();

            return(new WebPage(htmlResult));
        }