Beispiel #1
0
        private dynamic PostEx(string uri, IList <string> form,
                               NameValueCollection headers         = null,
                               SpecialHeaders specialHeaders       = null,
                               Func <String, dynamic> deserializer = null,
                               Encoding enc = null)
        {
            var     doPost    = new Func <dynamic>(() => this._http.Post(uri, form, headers, specialHeaders, deserializer, enc));
            var     needRetry = new Predicate <dynamic>(_ => _.ret_code != 0 && ((string)_.ret_msg).Contains("超时"));
            dynamic resp;

            while (needRetry(resp = doPost()))
            {
                this.Log?.Invoke($"{resp.ret_msg},正在重连。。。");
                this.Initialize();
                this.Log?.Invoke($"完成重连");
            }
            return(resp);
        }
        /// <summary>
        /// Gets the document type string for a given URL (note that this could actually download the page!)
        /// </summary>
        /// <param name="url">The url for which to get the document type</param>
        /// <returns>The entire document type string</returns>
        public static SpecialHeaders GetSpecialHeaders(Stream stream, string url)
        {
            SpecialHeaders specialHeaders = new SpecialHeaders();
            using (StreamReader reader = new StreamReader(stream))
            {
                // Read character by character until we hit the first brace
                // Doctype is always required to be the first tag in the page!
                StringBuilder builder = new StringBuilder();

                bool keepReading = true;
                char c;
                int i = 0;
                int foundCount = 0;

                while (keepReading)
                {
                    c = (char)reader.Read();
                    builder.Append(c);
                    if (c == '>')
                    {
                        string potentialMatch = builder.ToString().Trim();
                        if (potentialMatch.StartsWith(docTypeMatch, StringComparison.OrdinalIgnoreCase))
                            specialHeaders.DocType = potentialMatch;
                        else if (potentialMatch.StartsWith(savedFromMatch, StringComparison.OrdinalIgnoreCase))
                            specialHeaders.SavedFrom = potentialMatch;

                        builder = new StringBuilder();
                        foundCount++;
                    }

                    if (i > 1024 || foundCount > 2)
                        keepReading = false;

                    i++;
                }

            }

            return specialHeaders;
        }
        public static SpecialHeaders GetSpecialHeaders(IHTMLDocument2 document)
        {
            SpecialHeaders specialHeaders = new SpecialHeaders();
            IHTMLDocument3 doc3 = document as IHTMLDocument3;
            if (doc3 != null)
            {
                IHTMLDOMNode node = doc3.documentElement as IHTMLDOMNode;
                while (node.previousSibling != null)
                {
                    node = node.previousSibling;

                    IHTMLCommentElement commentElement = node as IHTMLCommentElement;
                    if (commentElement != null)
                    {
                        if (commentElement.text.StartsWith(docTypeMatch, StringComparison.OrdinalIgnoreCase))
                            specialHeaders.DocType = commentElement.text;

                        if (commentElement.text.StartsWith(savedFromMatch, StringComparison.OrdinalIgnoreCase))
                            specialHeaders.SavedFrom = commentElement.text;
                    }
                }

            }
            return specialHeaders;
        }