WinRT中的HttpUtility.ParseQueryString方法在哪里?

时间:2012-10-06 12:12:18

标签: http parsing windows-8 windows-runtime query-string

由于WinRT中没有HttpUtility,我想知道是否有一种解析HTTP查询字符串的简单方法?

WinRT中是否有一些等同于HttpUtility.ParseQueryString的内容?

1 个答案:

答案 0 :(得分:17)

而不是HttpUtility.ParseQueryString,您可以使用WwwFormUrlDecoder

以下是我抓住here

的示例
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];
        });
    }
}