哪里是在WinRT中的HttpUtility.ParseQueryString方法?(Where&#

2019-08-01 05:25发布

由于HttpUtility不可在WinRT中,我在想,如果有解析HTTP查询字符串一个简单的方法?

有一些实际上相当于HttpUtility.ParseQueryString在WinRT中?

Answer 1:

取而代之的HttpUtility.ParseQueryString可以使用WwwFormUrlDecoder

下面是我抓住了一个例子在这里

using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Windows.Foundation;

[TestClass]
public class Tests
{
    [TestMethod]
    public void TestWwwFormUrlDecoder()
    {
        Uri uri = new Uri("http://example.com/?a=foo&b=bar&c=baz");
        WwwFormUrlDecoder decoder = new WwwFormUrlDecoder(uri.Query);

        // named parameters
        Assert.AreEqual("foo", decoder.GetFirstValueByName("a"));

        // named parameter that doesn't exist
        Assert.ThrowsException<ArgumentException>(() => {
            decoder.GetFirstValueByName("not_present");
        });

        // number of parameters
        Assert.AreEqual(3, decoder.Count);

        // ordered parameters
        Assert.AreEqual("b", decoder[1].Name);
        Assert.AreEqual("bar", decoder[1].Value);

        // ordered parameter that doesn't exist
        Assert.ThrowsException<ArgumentException>(() => {
            IWwwFormUrlDecoderEntry notPresent = decoder[3];
        });
    }
}


文章来源: Where's the HttpUtility.ParseQueryString Method in WinRT?