【问题标题】:Get url parameters in Windows Phone Runtime 8.1在 Windows Phone Runtime 8.1 中获取 url 参数
【发布时间】:2014-05-24 12:30:42
【问题描述】:

我有一个这样的字符串(它是来自 Facebook 登录对话框的响应):

msft-{ProductID}://authorize/?access_token={user-access-token}&expires_in={expiration-time-of-token}

我需要获取 access_token 变量的值。

这里已经回答了同样的问题:Get url parameters from a string in .NET,但不幸的是,HttpUtility 在 Windows phone 8.1 运行时中不存在。有没有 HttpUtility.ParseQueryString 的替代品?

【问题讨论】:

    标签: c# windows-runtime windows-phone windows-store-apps windows-phone-8.1


    【解决方案1】:

    来自 MSDN 页面http://msdn.microsoft.com/en-us/library/windows/apps/hh825884.aspx

    使用 WwwFormUrlDecoder 类根据“&”符号的数量和位置将查询字符串分解为名称-值对。每个名称-值对由一个 IWwwFormUrlDecoderEntry 对象表示。

    【讨论】:

    • 太棒了!这就是我一直在寻找的。​​span>
    【解决方案2】:

    一种简单的方法是使用正则表达式。这应该有效:

            var regex = new System.Text.RegularExpressions.Regex("[?&]access_token(?:=(?<token>[^&]*))?");
    
            var match = regex.Match (responseString);
    
            if (match.Success)
            {
                Console.WriteLine (match.Groups["token"]);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-03
      相关资源
      最近更新 更多