由于WinRT中没有HttpUtility,我想知道是否有一种解析HTTP查询字符串的简单方法?
WinRT中是否有一些等同于HttpUtility.ParseQueryString的内容?
答案 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];
});
}
}