Пример #1
0
        protected override async Task OnProcessAsync(CancellationToken cancellationToken = default)
        {
            // HTTP URI.
            var url = "http://www.kingcean.net:8080/test/path?a=123&b=hello#nothing/all";
            var uri = HttpUri.Parse(url);

            Console.WriteLine(((Uri)uri).ToString());
            Console.WriteLine();

            // Query data.
            var query = "{ str: \"abcdefg\", b: true, \"name\": \"hijklmn\", // abcd: efg\n \"value\": \"012345\", \"num\": 67, null: undefined, \"props\": { \"x\": \"o\\tp\\tq\", \"y\": [ 8, 9, { \"z\": \"rst\" } ] } }";
            var q     = QueryData.Parse(query);

            Console.WriteLine(q.ToString());
            Console.WriteLine();

            // JSON HTTP web client.
            url = "https://github.com/compositejs/datasense/raw/main/package.json";
            var webClient = new JsonHttpClient <NameAndDescription>();
            var resp      = await webClient.SendAsync(HttpMethod.Get, url, cancellationToken);

            Console.WriteLine(resp.Name);
            resp = await webClient.SendAsync(HttpMethod.Get, url, cancellationToken);

            Console.WriteLine(resp.Name);

            //"{ \"access_token\": \"abc\", \"token_type\": \"Bearer\" }"
        }
Пример #2
0
        public void TestQueryData()
        {
            var json = @"{
    ""o"": [""abcdefg"", true, { ""uvw"": null }],
    ""p"": ""hijk\\\""lmn\"""", // rst
    ""q"": 1234567
}";
            var q    = QueryData.Parse(json);

            Assert.AreEqual(3, q.Count);
            Assert.AreEqual(q[0].Value, q["o"]);
            Assert.AreEqual(@"hijk\""lmn""", q[1].Value);
            Assert.AreEqual(q[1].Value, q["p"]);
            Assert.AreEqual("1234567", q[2].Value);
            Assert.AreEqual(q[2].Value, q["q"]);

            var query = "a=bcd\\efg+%20&hij=klmn&o=12345&p=67890&q=";

            q = QueryData.Parse(query);
            Assert.AreEqual(5, q.Count);
            Assert.AreEqual("bcd\\efg  ", q["a"]);
            Assert.AreEqual(q["a"], q[0].Value);
            Assert.AreEqual("klmn", q["hij"]);
            Assert.AreEqual(q["hij"], q[1].Value);
            Assert.AreEqual("12345", q[2].Value);
            Assert.AreEqual(q[2].Value, q["o"]);
            Assert.AreEqual("67890", q[3].Value);
            Assert.AreEqual(q[3].Value, q["p"]);
            Assert.AreEqual(string.Empty, q[4].Value);
            Assert.AreEqual(q[4].Value, q["q"]);
#if NETFRAMEWORK
            var backslash = "%5C";
#else
            var backslash = "%5c";
#endif
            Assert.AreEqual(query.Replace("%20", "+").Replace("\\", backslash), q.ToString());
        }