Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple way to get query string parameters from a URI in Windows Phone?

I'm currently working with a Custom URI Scheme to validate users using OAuth. In order to do this, I need to get the values of certain parameters from the query string.

Is there a simple way to get this information? Or is my only option to using REGEX or other string manipulation?

I have previously found references to things like ParseQueryString, but these are contained in libraries not available on Windows Phone.

like image 271
ShaneC Avatar asked Apr 08 '13 21:04

ShaneC


People also ask

What is query in URI?

The Query property contains any query information included in the URI. Query information is separated from the path information by a question mark (?) and continues to the end of the URI. The query information returned includes the leading question mark.

Is a query part of the URI?

A query string is a part of a uniform resource locator (URL) that assigns values to specified parameters.

How to pass in URL query string?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.


1 Answers

After a lot of searching I landed on a simple approach. So long as query strings are kept fairly simple (as they are in OAuth) this method should work.

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;

}
like image 81
ShaneC Avatar answered Oct 15 '22 09:10

ShaneC