Sha*_*neC 5 c# uri query-string windows-phone windows-phone-8
我目前正在使用自定义URI方案来验证使用OAuth的用户.为此,我需要从查询字符串中获取某些参数的值.
有没有一种简单的方法来获取这些信息?或者是我使用REGEX或其他字符串操作的唯一选择?
我以前发现了对ParseQueryString之类的引用,但这些引用包含在Windows Phone上不可用的库中.
经过大量的搜索,我找到了一个简单的方法.只要查询字符串保持相当简单(因为它们在OAuth中),此方法就可以工作.
public static Dictionary<string, string> ParseQueryString( string uri ) {
string substring = uri.Substring( ( ( uri.LastIndexOf('?') == -1 ) ? 0 : uri.LastIndexOf('?') + 1 ) );
string[] pairs = substring.Split( '&' );
Dictionary<string,string> output = new Dictionary<string,string>();
foreach( string piece in pairs ){
string[] pair = piece.Split( '=' );
output.Add( pair[0], pair[1] );
}
return output;
}
Run Code Online (Sandbox Code Playgroud)