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
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;
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.
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.
Related
This question already has answers here:
Get URL parameters from a string in .NET
(17 answers)
Closed 4 years ago.
I have a uri string like: http://example.com/file?a=1&b=2&c=string%20param
Is there an existing function that would convert query parameter string into a dictionary same way as ASP.NET Context.Request does it.
I'm writing a console app and not a web-service so there is no Context.Request to parse the URL for me.
I know that it's pretty easy to crack the query string myself but I'd rather use a FCL function is if exists.
Use this:
string uri = ...;
string queryString = new System.Uri(uri).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);
This code by Tejs isn't the 'proper' way to get the query string from the URI:
string.Join(string.Empty, uri.Split('?').Skip(1));
You can use:
var queryString = url.Substring(url.IndexOf('?')).Split('#')[0]
System.Web.HttpUtility.ParseQueryString(queryString)
MSDN
This should work:
string url = "http://example.com/file?a=1&b=2&c=string%20param";
string querystring = url.Substring(url.IndexOf('?'));
System.Collections.Specialized.NameValueCollection parameters =
System.Web.HttpUtility.ParseQueryString(querystring);
According to MSDN. Not the exact collectiontype you are looking for, but nevertheless useful.
Edit: Apparently, if you supply the complete url to ParseQueryString it will add 'http://example.com/file?a' as the first key of the collection. Since that is probably not what you want, I added the substring to get only the relevant part of the url.
I had to do this for a modern windows app. I used the following:
public static class UriExtensions
{
private static readonly Regex _regex = new Regex(#"[?&](\w[\w.]*)=([^?&]+)");
public static IReadOnlyDictionary<string, string> ParseQueryString(this Uri uri)
{
var match = _regex.Match(uri.PathAndQuery);
var paramaters = new Dictionary<string, string>();
while (match.Success)
{
paramaters.Add(match.Groups[1].Value, match.Groups[2].Value);
match = match.NextMatch();
}
return paramaters;
}
}
Have a look at HttpUtility.ParseQueryString() It'll give you a NameValueCollection instead of a dictionary, but should still do what you need.
The other option is to use string.Split().
string url = #"http://example.com/file?a=1&b=2&c=string%20param";
string[] parts = url.Split(new char[] {'?','&'});
///parts[0] now contains http://example.com/file
///parts[1] = "a=1"
///parts[2] = "b=2"
///parts[3] = "c=string%20param"
For isolated projects, where dependencies must be kept to a minimum, I found myself using this implementation:
var arguments = uri.Query
.Substring(1) // Remove '?'
.Split('&')
.Select(q => q.Split('='))
.ToDictionary(q => q.FirstOrDefault(), q => q.Skip(1).FirstOrDefault());
Do note, however, that I do not handle encoded strings of any kind, as I was using this in a controlled setting, where encoding issues would be a coding error on the server side that should be fixed.
In a single line of code:
string xyz = Uri.UnescapeDataString(HttpUtility.ParseQueryString(Request.QueryString.ToString()).Get("XYZ"));
Microsoft Azure offers a framework that makes it easy to perform this.
http://azure.github.io/azure-mobile-services/iOS/v2/Classes/MSTable.html#//api/name/readWithQueryString:completion:
You could reference System.Web in your console application and then look for the Utility functions that split the URL parameters.
Iam trying to encode a url, so that the HttpWebRequest is fine with characters like &.
So google bring me up to this:
url = HttpUtility.UrlEncode(url);
But this makes the whole url unuseable. Iam getting Status-Error: Invalid Operation from Web-Server.
I got this url before iam using encoding:
http://jira-test.myServer.de/rest/api/2/search?jql=labels = "F&E"
After encoding i got this:
http%3a%2f%2fjira-test.brillux.de%2frest%2fapi%2f2%2fsearch%3fjql%3dlabels+%3d+%22F%26E%22
What iam doing wrong? In my opinion it shouldn't replace the // after http and so on... Or is there another way to handle this issue?
Info:
Uri.EscapeDataString();
gives me the same result.
You should only be encoding the values of your query string, not the entire URI:
var uri = "http://jira-test.myServer.de/rest/api/2/search?jql=" +
HttpUtility.UrlEncode("labels = \"F&E\"");
// Result: http://jira-test.myServer.de/rest/api/2/search?jql=labels+%3d+%22F%26E%22
The proper way to construct this:
// Construct query string using HttpValueCollection, which handles escaping:
var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString.Add("jql", "labels = \"F&E\"");
// Combine base URI with query string through UriBuilder:
var uriBuilder = new UriBuilder("http://jira-test.myServer.de/rest/api/2/search");
uriBuilder.Query = queryString.ToString();
// Get string representation:
string uri = uriBuilder.ToString();
// Result: http://jira-test.myserver.de:80/rest/api/2/search?jql=labels+%3d+%22F%26E%22
I have an application where uses post comments. Security is not an issue.
string url = http://example.com/xyz/xyz.html?userid=xyz&comment=Comment
What i want is to extract the userid and comment from above string.
I tried and found that i can use IndexOf and Substring to get the desired code BUT what if the userid or comment also has = symbol and & symbol then my IndexOf will return number and my Substring will be wrong.
Can you please find me a more suitable way of extracting userid and comment.
Thanks.
I got url using string url =
HttpContext.Current.Request.Url.AbsoluteUri;
Do not use AbsoluteUri property , it will give you a string Uri, instead use the Url property directly like:
var result = System.Web.HttpUtility.ParseQueryString(HttpContext.Current.Request.Url.Query);
and then you can extract each parameter like:
Console.WriteLine(result["userid"]);
Console.WriteLine(result["comment"]);
For other cases when you have string uri then do not use string operations, instead use Uri class.
Uri uri = new Uri(#"http://example.com/xyz/xyz.html?userid=xyz&comment=Comment");
You can also use TryCreate method which doesn't throw exception in case of invalid Uri.
Uri uri;
if (!Uri.TryCreate(#"http://example.com/xyz/xyz.html?userid=xyz&comment=Comment", UriKind.RelativeOrAbsolute, out uri))
{
//Invalid Uri
}
and then you can use System.Web.HttpUtility.ParseQueryString to get query string parameters:
var result = System.Web.HttpUtility.ParseQueryString(uri.Query);
The ugliest way is the following:
String url = "http://example.com/xyz/xyz.html?userid=xyz&comment=Comment";
usr = url.Split('?')[1];
usr= usr.Split('&')[0];
usr = usr.Split('=')[1];
But #habib version is better
I have a winform application, and I would like to parse a string that represent an URL to extract some parameters.
a sample of the URL is this:
http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e
the parameter I would like to extract is 271443634510 (that is, the last part of the path before the query string).
Any idea ho how this can be done?
You can use Uri.Segments, which splits up the stuff after your domain into an array that includes, for your example:
/
itm/
Sector-Watch/
271443634510
So all you need to get is the item at index 3. Working example:
string url = "http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e";
Uri uri = new Uri(url);
var whatYouWant = uri.Segments[3];
You can do this:
string url = "http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e";
string parameter = Regex.Match(url,"\d+(?=\?)|(?!/)\d+$").Value;
You can simply use Split function (tested and verified):
string MyUrl="http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e";
string str=MyUrl.Split('/').Last().Split('?').First();
I need to strip out any "&id=SomeValue" from a Url.PathAndQuery. Where SomeValue could be an int or a string. And it may or may not be followed by another ampersand.
So it could be
somepage.aspx?cat=22&id=SomeId¶m2=4
or
somepage.aspx?cat=tect&id=450
I want to be left with
somepage.aspx?cat=22¶m2=4
or
somepage.aspx?cat=tect
Just going off the top of my head...
string url = "somepage.aspx?cat=22&id=SomeId¶m2=4";
Regex regex = new Regex("([\?\&])id=[^\?\&]+");
url = regex.replace(url, "\1");
System.Diagnostics.Debug.WriteLine("url = " + url);
Update 2010-03-05 11:12 PM PST
I've been shamed by a comment into actually testing my code. What are you, my QA department? Here's a working example using MSTest.
Regex regex = new Regex(#"([\?\&])id=[^\&]+[\&]?");
[TestMethod]
public void RegexReplacesParameterInMiddle()
{
string url = "somepage.aspx?cat=22&id=SomeId¶m2=4";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?cat=22¶m2=4",url);
}
[TestMethod]
public void RegexReplacesParameterInFront()
{
string url = "somepage.aspx?id=SomeId&cat=22¶m2=4";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?cat=22¶m2=4", url);
}
[TestMethod]
public void RegexReplacesParameterAtEnd()
{
string url = "somepage.aspx?cat=22¶m2=4&id=SomeId";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?cat=22¶m2=4&", url);
}
[TestMethod]
public void RegexReplacesSoleParameter()
{
string url = "somepage.aspx?id=SomeId";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?", url);
}
public void RegexIgnoresMissingParameter()
{
string url = "somepage.aspx?foo=bar&blet=monkey";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?foo=bar&blet=monkey", url);
}
The regex, interpreted, says:
Look for a "?" or an "&" character (and store it as a backreference)
followed by "id="
followed by one or more non-"&" characters.
optionally followed by another "&"
Then replace that expression with the backreference, so you don't lose your initial ?/&.
note -- as you can see from the tests, this emits a trailing ? or & when the replaced parameter is the only one or the last one, respectively. You could use string methods to get rid of that, though if somebody knows how to keep them out of the result using only regular expressions it would be excellent to see.
If bad things could happen (e.g., security-wise) if an "id=" parameter were missed by the regular expression, then you also need to worry that the query string might contain a hexadecimal urlencoded equivalent, which the regular expression will not recognize. For example, "id" is "%69%64". Also consider the effects of different capitalizations of "id" on your program. My opinion in this situtation is that you read the RFCs and build a complete class that can do transformations in both directions from a set of name-value pairs to a query strings. System.Uri will not do this. if you are running inside an ASP.NET application, you might investigate if HttpUtility.ParseQueryString is sufficient.
I would first parse the Querystring to Strongly typed values, then I would check using Regex if I needed to.
C# ASP.NET QueryString parser