I have a website that includes a language specifier in the URL (ex. http://example.org/English/rest/of/url.aspx)
Using a Regex, I can parse out the language of the URL:
Match match = Regex.Match(HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath, "^~/(?<language>[^/]+)/");
I can then check the parsed out language and determine if the language is what I want it to be:
match.Groups["language"].Value
I'm now looking for a simpler way than brutish string manipulation to replace only that language with a new language if needed.
So the URL above would be changed to http://example.org/German/rest/of/url.aspx
My initial thought was a simple search/replace however that won't work as the page name or other URL fragments may have the language name in them. I'm only concerned with the very first fragment after the root URL.
After changing the URL I would then redirect the user and be done with it.
Or you can use Regex.Replace:
string requestUrl = "~/English/rest/of/url.aspx";
string targetLanguage = "German";
Match match = Regex.Match(requestUrl, "^~/(?<language>[^/]+)/");
if (match.Groups["language"].Value != targetLanguage)
Response.Redirect(Regex.Replace(requestUrl, "^~/[^/]+/", string.Format("~/{0}/", targetLanguage)));
you can create a Uri object then use PathAndQuery to extract the then path split '/' and replace the first instance then construct your uri again
Related
Question:
I need to create Uri which Contains all its attributes like (OriginalString, Query) as URL encoded.
What I tried:
I am able to format input string of Uri with below options:
string encodedString = inputString.Replace("'", "%27"); //example encoding
OR
System.Net.WebUtility.UrlEncode (inputString)
Tried all approaches to convert string to encoded string, like EscapeDataString/EscapeUriString etc
What I need to achieve
var uri = new Uri(encodedString);
When I create new Uri (like above) again %27 is replaced by '.
UPDATE 1:
Not all fields of uri are converted into %27. OriginalString is converted in my case.
I want to pass this uri to HttpClient.
Do we have any mechanism to make Uri with encoded string.
Based on locale settings and used keyboard, iOS may use some other apostrophe-like character, for example right single quotation mark U+2019 (').
Solution was to handle that.
Give it a try with HttpUtility.UrlEncode(input string).
Refer to https://secretgeek.net/uri_enconding for more details.
I have a URL and need to extract the port, username and password from it and put them into an array. It looks like following.
http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts
Can I use some other method without replaces or substring?
One of the ways in C#
Get the query parameter
var parsedQuery = HttpUtility.ParseQueryString("http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts");
Then, below will give the username
parsedQuery["username"]
For Password:
parsedQuery["password"]
For port you can use URI :
Uri uri = new Uri("http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts");
Get the port by
uri.Port
Create an array or use whatever you require to club.
I don't know C#, but here's one that works for Python. It's pretty straightforward so you should be able to convert.
:(?P<port>[0-9]+).*username=(?P<username>[a-zA-Z0-9]+).*password=(?P<password>[a-zA-Z0-9]+)
The (?P<foo>bar) syntax is a named capture group that will put a variable matching the pattern 'bar' into a variable called 'foo' when you extract them.
Here is another possible solution with pure C# regex:
var url = "http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts";
var urlRegex = new Regex(#"(?<=(http(s)?://)?\w+(\.\w+)*:)\d+(?=/.*)?");
var usernameRegex = new Regex(#"(?<=(\?|&)username=).*?(?=&|$)", RegexOptions.IgnoreCase);
var passwordRegex = new Regex(#"(?<=(\?|&)password=).*?(?=&|$)", RegexOptions.IgnoreCase);
Console.WriteLine(urlRegex.Match(url));
Console.WriteLine(usernameRegex.Match(url));
Console.WriteLine(passwordRegex.Match(url));
If there are any parts that don't change, e.g. if it's always the same url you could just replace it like this
string str = "http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts"
str.Replace("http://myproject.ddns.net","");
This would leave you ":8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts"
There is nothing stopping you repeating the process with another section.
As for regex you could use Regex.Match https://msdn.microsoft.com/en-us/library/twcw2f1c(v=vs.110).aspx to get the parts you want.
You could use ":\d{4}/" to get the port - you'd have to strip the leading ":" and trailing "/" though; this "username=\w*\&" to get the username - you'd have to strip the leading "username=" and trailing "&" though; and for the password you could use "password=\w*\&" - you'd have to strip the leading "password=" and trailing "&" though.
If you'd like to experiment with regex this site https://regex101.com/ is pretty good.
I have a repeater.Insidea a repeater there is a button.On item Command I try
protected void rptPost_ItemCommand(object source, RepeaterCommandEventArgs e)
{
int contentID = Int32.Parse(e.CommandArgument.ToString());//Its return 1
string host = HttpContext.Current.Request.Url.ToString();//its return http://localhost:1377/Forum.aspx
//string URL = "~/Modules/Forum/PostDetails.aspx?ID=" + contentID +"&BackUrl=" + host;//Need help hear its not work
string URL = "~/Modules/Forum/PostDetails.aspx?ID=" + contentID ;//Its work
Response.Redirect(URL);
}
when I only try to pass ID its work but if I try to pass multipal url one is ID and next is Current Page Url its unable to find page.And gives page not found error.I unable to understand the problem.Passing Url as a parameter is not support or I missing some thing.Thanks.
Here is the solution:
host = HttpUtility.UrlEncode(host)
and here is why it works:
You are passing the host URL as a GET parameter. GET parameters are part of the GET URI for the redirect. URI syntax includes characters with special meanings, in particular you are using colon (:) and slash (/), which have special meaning in a URI string. In almost all cases special characters should not be used as part of your GET parameter data values.
For cases such as yours, where there is a need to include special characters, UrlEncoding was developed. UrlEncoding, or Percent-encoding, substitutes special characters in URIs with values encoded using the percent sign. These escape sequences are automatically converted back to the original special character as part of URI query processing. The HttpUtility class provides the UrlEncode method as a convenient way to escape all special characters in an input string.
Please note that it is a good idea to URL encode all data that you pass via GET parameters unless you have full control over the data being passed and you can guarantee that encoding is not necessary. This can be a security issue because attackers can include new parameters and otherwise manipulate your URI if you pass user input to a GET parameter without first URL encoding it.
In my application, I must read a URL and do something if the URL contains Basic authentication credentials. An example of such a URL is
http://username:password#example.com
Is the regular expression below a good fit for my task? I am to capture four groups into local variables. The URL is passed to another internal library that will do further work to ensure the URL is valid before opening a connection.
^(.+?//)(.+?):(.+?)#(.+)$
It looks ok, and I think that a regular expression is good to use in this case. A couple of suggestions:
1) I think that named groups would make your code more readable, i.e:
^(?<protocol>.+?//)(?<username>.+?):(?<password>.+?)#(?<address>.+)$
Then you can simply write
Match match = Regex.Match(string, pattern);
if (match.Success) {
string user = match.Groups["username"];
2) then you could make the expression a little more strict, e.g. using \w when possible instead of .:
^(?<protocol>\w+://)...
Your regex seems OK, but why not use the thoroughly-tested and nearly-compliant Uri class? It's then trivial to access the pieces you want without worrying about spec-compatibility:
var url = new Uri("http://username:password#example.com");
var userInfo = url.UserInfo.Split(':');
var username = userInfo[0];
var password = userInfo[1];
Given this regex:
^((https?|ftp):(\/{2}))?(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))|(((([a-zA-Z0-9]+)(\.)*?))(\.)([a-z]{2}
|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum){1})
Reformatted for readability:
#"^((https?|ftp):(\/{2}))?" + // http://, https://, ftp:// - Protocol Optional
#"(" + // Begin URL payload format section
#"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" + // IPv4 Address support
#")|("+ // Delimit supported payload types
#"((([a-zA-Z0-9]+)(\.)*?))(\.)([a-z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum){1}" + // FQDNs
#")"; // End URL payload format section
How can I make it fail (i.e. not match) on this "fail" test case?
http://www.google
As I am specifying {1} on the TLD section, I would think it would fail without the extension. Am I wrong?
Edit: These are my PASS conditions:
"http://www.zi255.com?Req=Post&PID=4",
"http://www.zi255.com?Req=Post&ID=4",
"http://www.zi255.com/?Req=Post&PID=4",
"http://www.zi255.com?Req=Post&PostID=4",
"http://www.zi255.com/?Req=Post&ID=4"
"http://www.zi255.com?Req=Post&Post=4",
"http://www.zi255.com?Req=Post&Entry=4",
"http://www.zi255.com?PID=4"
"http://www.zi255.com/Post.aspx?Req=Post&ID=4",
"http://www.zi255.com/Post.aspx?Req=Post&PID=4",
"http://www.zi255.com/Post.aspx?Req=Post&Post=4",
"http://www.zi255.com/Post.aspx?Req=Post&Title=Random%20Post%20Name"
"http://www.zi255.com/?Req=Post&Title=Random%20Post%20Name",
"http://www.zi255.com?Req=Post&Title=Random%20Post%20Name",
"http://www.zi255.com?Req=Post&PostID=4",
"http://www.zi255.com?Req=Post&Post=4",
"http://www.zi255.com?Req=Post&Entry=4",
"http://www.zi255.com?PID=4"
"http://www.zi255.com",
"http://www.damnednice.com"
These are my FAIL conditions:
"http://.com",
"http://.com/",
"http:/www.google.com",
"http:/www.google.com/",
"http://www.google",
"http://www.googlecom",
"http://www.google.c",
".com",
"https://www..."
I'll throw out an alternative suggestion. You may want to use a combination of the parsing of the built-in System.Uri class and a couple targeted regexes (or simple string checks when appropriate).
Example:
string uriString = "...";
Uri uri;
if (!Uri.TryCreate(uriString, UriKind.Absolute, out uri))
{
// Uri is totally invalid!
}
else
{
// validate the scheme
if (!uri.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
{
// not http!
}
// validate the authority ('www.blah.com:1234' portion)
if (uri.Authority // ...)
{
}
// ...
}
Sometimes, one catch-all reqex is not the best solution, however tempting. While debugging this regex is feasible (see Greg Hewgills answer), consider doing a couple of tests for different categories of problems, e.g. one test for numerical addresses and one test for named addresses.
You need to force your regex to match up until the end of the string. Add a $ at the very end of it. Otherwise, your regex is probably just matching http://, or something else shorter than your whole string.
The "validate a url" problem has been solved* numerous times. I suggest you use the System.Uri class, it validates more cases than you can shake a stick at.
The code Uri uri = new Uri("http://whatever"); throws a UriFormatException if it fails validation. That is probably what you'd want.
*) Or kind of solved. It's actually pretty tricky to define what is a valid url.
Its all about definitions, a "valid url" should provide you with a IP address when you do a DNS Lookup. The IP should be connected to and when a request is send out, you get a reply in the form of a HTML information that you can use.
So what we are looking for is a "valid URL Format" and that is where the system.uri comes in very handy. BUT, if the URL is hidden in a large piece of tekst, you would first like to find something that validates as a valid URL-Format.
The thing that distinquishes a URL from any given readable tekst is the dot not followed by whitespace. "123.com" could validate as a real URL.
Using the regex
[a-z_\.\-0-9]+\.[a-z]+[^ ]*
to find any possible valid url in a text and then do a system.uri check to see if its a valid URL format and then do a lookup. Only when the lookup gives you a result then you know the URL is valid.