Пример #1
0
        public static CQ CreateFromUrl(string url)
        {
            CsqWebRequest con = new CsqWebRequest(url);

            con.Get();
            con.UserAgent = UserAgent;
            return(CQ.Create(con.Html));
        }
Пример #2
0
        /// <summary>
        /// Creates a new DOM from an HTML file.
        /// </summary>
        ///
        /// <param name="url">
        /// The URL of the remote server.
        /// </param>
        /// <param name="options">
        /// The options to use when creating the reqest.
        /// </param>
        ///
        /// <returns>
        /// A CQ object composed from the HTML response from the server.
        /// </returns>

        public static CQ CreateFromUrl(string url, ServerConfig options = null)
        {
            CsqWebRequest request = new CsqWebRequest(url);

            ServerConfig.Apply(options, request);

            request.Get();

            return(CQ.CreateDocument(request.Html));
        }
Пример #3
0
        public void Utf8NoContentType()
        {
            var creator = new Mocks.MockWebRequestCreator();

            creator.CharacterSet   = "ISO-8859-1";
            creator.ResponseStream = GetMemoryStream(TestHtml("arabic"), new UTF8Encoding(false));

            CsqWebRequest request = new CsqWebRequest("http://test.com", creator);

            // remove the content type header
            var html = ReplaceCharacterSet(request.Get());

            var dom = CQ.CreateDocument(html);

            Assert.AreNotEqual(arabicExpected, dom["h1"].Text());

            //test synchronous: this is the code that CreateFromURL uses

            creator.CharacterSet = null;
            request = new CsqWebRequest("http://test.com", creator);

            var httpRequest    = request.GetWebRequest();
            var response       = httpRequest.GetResponse();
            var responseStream = response.GetResponseStream();
            var encoding       = CsqWebRequest.GetEncoding(response);
            var dom2           = CQ.CreateDocument(responseStream, encoding);

            Assert.AreEqual(arabicExpected, dom2["h1"].Text());

            // Test async version now

            request = new CsqWebRequest("http://test.com", creator);

            bool?done = null;
            CQ   dom3 = null;

            request.GetAsync((r) =>
            {
                dom3 = r.Dom;
                done = true;
            }, (r) => {
                done = false;
            });

            while (done == null)
            {
                ;
            }
            Assert.IsTrue((bool)done);
            Assert.AreEqual(arabicExpected, dom3["h1"].Text());
        }
Пример #4
0
        public void ContentTypeHeader()
        {
            var creator = new Mocks.MockWebRequestCreator();

            creator.CharacterSet   = "windows-1255";
            creator.ResponseStream = GetMemoryStream(htmlStart + htmlStart3 + hebrewChar + htmlEnd, Encoding.GetEncoding("windows-1255"));

            CsqWebRequest request = new CsqWebRequest("http://test.com", creator);

            var dom1 = CQ.Create(request.Get());

            creator.CharacterSet = "";
            request = new CsqWebRequest("http://test.com", creator);
            var dom2 = CQ.Create(request.Get());

            var output = dom1.Render(OutputFormatters.HtmlEncodingMinimum);

            // The characters should be encoded differently.

            var outputHebrewChar = dom1["#test"].Text();
            var outputUTF8Char   = dom2["#test"].Text();

            Assert.AreNotEqual(outputHebrewChar, outputUTF8Char);

            // try it again, using the meta tag
            creator.CharacterSet   = "windows-1255";
            creator.ResponseStream = GetMemoryStream(htmlStart + htmlStartMeta + htmlStart3 + hebrewChar + htmlEnd, Encoding.GetEncoding("windows-1255"));

            /// CreateFromUrl process

            request = new CsqWebRequest("http://test.com", creator);
            var httpRequest    = request.GetWebRequest();
            var response       = httpRequest.GetResponse();
            var responseStream = response.GetResponseStream();
            var encoding       = CsqWebRequest.GetEncoding(response);

            var dom3 = CQ.CreateDocument(responseStream, encoding);
            var outputHebrewChar2 = dom3["#test"].Text();

            Assert.AreEqual(outputHebrewChar, outputHebrewChar2);
        }