public void ReceiveHeader()
        {
            Header  = "";
            Headers = new WebHeaderCollection();

            byte[] bytes = new byte[10];
            while (socket.Receive(bytes, 0, 1, SocketFlags.None) > 0)
            {
                if (EncodingType != null)
                {
                    Header += EncodingType.GetString(bytes, 0, 1);
                }
                else
                {
                    Header += Encoding.UTF8.GetString(bytes, 0, 1);
                }
                if (bytes[0] == '\n' && Header.EndsWith("\r\n\r\n"))
                {
                    break;
                }
            }
            MatchCollection matches = new Regex("[^\r\n]+").Matches(Header.TrimEnd('\r', '\n'));

            for (int n = 1; n < matches.Count; n++)
            {
                string[] strItem = matches[n].Value.Split(new char[] { ':' }, 2);
                if (strItem.Length > 0)
                {
                    Headers[strItem[0].Trim()] = strItem[1].Trim();
                }
            }
            // check if the page should be transfered to another location
            if (matches.Count > 0 && (
                    matches[0].Value.IndexOf(" 302 ") != -1 ||
                    matches[0].Value.IndexOf(" 301 ") != -1))
            {
                // check if the new location is sent in the "location" header
                if (Headers["Location"] != null)
                {
                    try { ResponseUri = new Uri(Headers["Location"]); }
                    catch { ResponseUri = new Uri(ResponseUri, Headers["Location"]); }
                }
            }
            ContentType = Headers["Content-Type"];
            if (Headers["Content-Length"] != null)
            {
                ContentLength = int.Parse(Headers["Content-Length"]);
            }
            KeepAlive = (Headers["Connection"] != null && Headers["Connection"].ToLower() == "keep-alive") ||
                        (Headers["Proxy-Connection"] != null && Headers["Proxy-Connection"].ToLower() == "keep-alive");
        }
Exemplo n.º 2
0
        public void SlackAlert(Exception exception, string context, [CallerMemberName] string memberName = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0)
        {
            lock (lockObj)
            {
                Error error = new Error
                {
                    Context      = context,
                    ErrorMessage = exception,
                    Site         = Site,
                    MemberName   = memberName,
                    FileName     = fileName,
                    LineNumber   = lineNumber
                };

                string alert = string.Empty;
                alert += "Site Error" + Environment.NewLine;
                alert += DateTime.Now.ToLongDateString() + Environment.NewLine;
                alert += "Site: " + error.Site + Environment.NewLine;
                alert += "Context: " + error.Context + Environment.NewLine;
                alert += "Message: " + error.ErrorMessage.Message + Environment.NewLine;
                alert += "Method: " + error.MemberName + Environment.NewLine;
                alert += "File: " + error.FileName + Environment.NewLine;
                alert += "Line: " + error.LineNumber.ToString() + Environment.NewLine;

                Payload payload = new Payload
                {
                    Channel      = Channel,
                    Username     = UserName,
                    AlertMessage = alert
                };

                string payloadJson = JsonConvert.SerializeObject(payload);

                using (WebClient webClient = new WebClient())
                {
                    NameValueCollection nameValueCollection = new NameValueCollection
                    {
                        ["payload"] = payloadJson
                    };
                    byte[] response     = webClient.UploadValues(SlackUrlWithAccessToken, "POST", nameValueCollection);
                    string responseText = EncodingType.GetString(response);
                }
            }
        }