简体   繁体   English

在Windows Phone Runtime 8.1中获取url参数

[英]Get url parameters in Windows Phone Runtime 8.1

I have a string like this one (it's the response from the Facebook login dialog): 我有一个像这样的字符串(这是Facebook登录对话框的响应):

msft-{ProductID}://authorize/?access_token={user-access-token}&expires_in={expiration-time-of-token} msft- {的ProductID}://授权/ =的access_token {用户访问令牌}&expires_in = {期满的时的令牌}?

And I need to get the value of the access_token variable. 我需要获取access_token变量的值。

It's the same issue already answered here: Get url parameters from a string in .NET , but unfortunately the HttpUtility doesn't exist in Windows phone 8.1 runtime. 这里已经回答了同样的问题: 从.NET中的字符串中获取url参数 ,但不幸的是,Windows Phone 8.1运行时中不存在HttpUtility。 Is there an alternative to HttpUtility.ParseQueryString? 有没有替代HttpUtility.ParseQueryString?

From MSDN page http://msdn.microsoft.com/en-us/library/windows/apps/hh825884.aspx : 从MSDN页面http://msdn.microsoft.com/en-us/library/windows/apps/hh825884.aspx

Use the WwwFormUrlDecoder class to break a query string into name-value pairs, based on the number and placement of "&" symbols. 根据“&”符号的数量和位置,使用WwwFormUrlDecoder类将查询字符串分解为名称 - 值对。 Each name-value pair is represented by an IWwwFormUrlDecoderEntry object. 每个名称 - 值对由IWwwFormUrlDecoderEntry对象表示。

One easy way is to use Regular Expression. 一种简单的方法是使用正则表达式。 This should work: 这应该工作:

        var regex = new System.Text.RegularExpressions.Regex("[?&]access_token(?:=(?<token>[^&]*))?");

        var match = regex.Match (responseString);

        if (match.Success)
        {
            Console.WriteLine (match.Groups["token"]);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM