17

My requirement is to parse Http Urls and call functions accordingly. In my current implementation, I am using nested if-else statement which i think is not an optimized way. Can you suggest some other efficient approch?

Urls are like these:

  • server/func1
  • server/func1/SubFunc1
  • server/func1/SubFunc2
  • server/func2/SubFunc1
  • server/func2/SubFunc2
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
vijay053
  • 822
  • 3
  • 18
  • 36
  • What about split the url's and get only the last token, if you only need the function names? – SamGamgee Feb 04 '13 at 10:55
  • 1
    Can you be clearer on what you mean by "parse"? I assume there's some specific data you want to extract; but what part and for which purpose? – Eamon Nerbonne Feb 04 '13 at 10:55
  • There are many kind of urls which I get. It contains attributes, useful value in uris also. – vijay053 Feb 04 '13 at 11:02
  • Is it something like this you want http://stackoverflow.com/questions/70405/does-c-sharp-have-a-string-tokenizer-like-javas ? You can split the url's in the '/' and go through each token. – SamGamgee Feb 04 '13 at 11:15
  • But again I have to use nested if else statements... – vijay053 Feb 04 '13 at 11:20
  • There are alternatives to nested if/else. `switch` comes to mind, as does using a table lookup. But without more information about what you're doing, it'll be impossible to give you any kind of recommendation. – Jim Mischel May 20 '13 at 19:35

3 Answers3

54

I think you can get a lot of use out of the System.Uri class. Feed it a URI and you can pull out pieces in a number of arrangements.

Some examples:

Uri myUri = new Uri("http://server:8080/func2/SubFunc2?query=somevalue");

// Get host part (host name or address and port). Returns "server:8080".
string hostpart = myUri.Authority;

// Get path and query string parts. Returns "/func2/SubFunc2?query=somevalue".
string pathpart = myUri.PathAndQuery;

// Get path components. Trailing separators. Returns { "/", "func2/", "sunFunc2" }.
string[] pathsegments = myUri.Segments;

// Get query string. Returns "?query=somevalue".
string querystring = myUri.Query;
Suncat2000
  • 1,105
  • 1
  • 14
  • 17
7

This might come as a bit of a late answer but I found myself recently trying to parse some URLs and I went along using a combination of Uri and System.Web.HttpUtility as seen here, my URLs were like http://one-domain.com/some/segments/{param1}?param2=x.... so this is what I did:

var uri = new Uri(myUrl);
string param1 = uri.Segments.Last();

var parameters = HttpUtility.ParseQueryString(uri.Query);
string param2 = parameters["param2"];

note that in both cases you'll be working with strings, and be specially weary when working with segments.

Luiso
  • 4,173
  • 2
  • 37
  • 60
0

I combined the split in Suncat2000's answer with string splitting to get at interesting features of the URL. I am passing in a full Uri including https: etc. from another page as the navigation argument e.Parameter:

Uri playlistUri = (Uri)e.Parameter;
string youtubePlaylistUnParsed = playlistUri.Query;
char delimiterChar = '=';
string[] sections = youtubePlaylistUnParsed.Split(delimiterChar);
string YoutubePlaylist = sections[1];

This gets me the playlist in the PLs__ etc. form for use in the Google APIs.

Tim
  • 1,108
  • 13
  • 25