How do I stop System.Uri from unencoding a URL? - c#

How do I stop the System.Uri class from unencoding an encoded URL passed into its constructor? Consider the following code:-
Uri uri = new Uri("http://foo.bar/foo%2FBar");
Console.WriteLine(uri.AbsoluteUri);
uri = new Uri("http://foo.bar/foo%2FBar", false);
Console.WriteLine(uri.AbsoluteUri);
uri = new Uri("http://foo.bar/foo%2FBar", true);
Console.WriteLine(uri.AbsoluteUri);
In each case the output is "http://foo.bar/foo/bar". How can I ensure that after instantiating a Uri instance, Uri.AbsoluteUri will return "http://foo.bar/foo%2FBar"?

The Uri class isn't used to generate an escaped URI, although you can use uri.OriginalString to retrieve the string used for initialization. You should probably be using UrlEncode if you need to reencode a URI safely.
Also, as per http://msdn.microsoft.com/en-us/library/9zh9wcb3.aspx the dontEscape parameter in the Uri initializer has been deprecated and will always be false.
UPDATE:
Seems someone found a way (hack) to do this - GETting a URL with an url-encoded slash

Related

Uri - append path [duplicate]

I am retro-fitting an application to make use of a PHP HTTP proxy (for caching) instead of the actual API server, the application currently combines the server URI and the path with the code:
methodUri = new Uri(apiUri, method.Path)
Where:
apiUri = "http://api.eve-online.com/" (System.Uri Object)
method.Path = "/char/SkillIntraining.xml.aspx" (string)
The result of the above statement is
"http://api.eve-online.com/char/SkillIntraining.xml.aspx" (System.Uri Object)
To use the PHP HTTP proxy the request would have to be changed as follows
apiUri = "http://www.r-s.co.uk/eproxy.php" (System.Uri Object)
method.Path = "/char/SkillIntraining.xml.aspx" (string)
The output I was expecting was:
"http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx" (System.Uri Object)
However the output I get is:
"http://www.r-s.co.uk/char/SkillIntraining.xml.aspx" (System.Uri Object)
I understand that this is the correct functionality of the constructor Uri(Uri, string), my question is what would be a better function or constructor to use in its place to get the output I expect? I have tried removing the leading "/" in method.Path taking it from an absolute path to a relative path however that did not help.
NOTE: both solutions below do work, however System.UriBuilder provides a more robust mechanism for combining URI's and paths and in my case resulted in fewer changes to resources than using System.Uri. Had I the choice I would mark both answers as correct.
Don't use the Uri object, use a UriBuilder - it copes way better with missing slashes
So
Uri apiUri = new Uri("http://www.r-s.co.uk/eproxy.php");
string methodPath = "/char/SkillIntraining.xml.aspx";
System.UriBuilder uriBuilder = new System.UriBuilder(apiUri);
uriBuilder.Path += methodPath;
Console.WriteLine(uriBuilder.Uri.ToString());
works as expected and produces http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx
Add a trailing "/" to apiUri, and remove the leading "/" from method.Path:
Uri apiUri = new Uri("http://www.r-s.co.uk/eproxy.php/");
string path = "char/SkillIntraining.xml.aspx";
Uri uri = new Uri(apiUri, path);
Console.WriteLine(uri.ToString());
Will print:
http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx

'System.UriFormatException' in HyperLink

I have some strings like this:
www.example.com/sdWqaP
twitter.com/sdfks
and want to assign them to a HyperLink
var hyperlink = new Hyperlink
{
NavigateUri = new Uri(url),
TargetName = "_blank",
};
if url starts with http:// it works fine, otherwise throws a UriFormatException.
Update: urls like this www.google.com aren't valid http urls. isn't there a better way than var url = "http://" + "www.google.com"
You can use
var uri = new UriBuilder(s).Uri;
Reference: http://msdn.microsoft.com/en-us/library/y868d5wh(v=vs.110).aspx
public UriBuilder(
string uri
)
// If uri does not specify a scheme, the scheme defaults to "http:".
Scheme (http:// in your case) is mandatory part of Uri string. UriFormatException will be thrown if the scheme specified in uri string is not correctly formed according to Uri.CheckSchemeName() method.
[MSDN : Uri Constructor (String)].
I don't understand well what you mean "better safer way". Appending scheme in uri string is common practice anyway.
Check your URL is valid and then assign to the URL
For validating a URL check the below link
How to check whether a string is a valid HTTP URL?

Uri constructor with dontEscape is obsolete, what is alternatieve?

My question is regarding passing an URL to HttpWebRequest without escaping, I searched the forums and internet, but I didn't find a good solution for it.
I have following URL:string URL= www.website.com/sub/redirec\t\bs\dd
So when I create an uri like this:
Uri uri = new Uri(URL);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
In this case on a get method I will get following URL:www.website.com/sub/redirect%5Ct%5Cbc%5Cdd
This sign "\" will be replaced by "%5C". What is crucial for me not to happen?
I can avoid that by:
Uri uri = new Uri(URL, true); //bool dontEscape
But this constructor is obsolete. How to have same effect without using obsolete?
use this
Uri uri = new Uri(Uri.EscapeUriString(URL));

.NET URI: How can I change ONE part of a URI?

Often I want to change just one part of a URI and get a new URI object back.
In my current dilemma, I want to append .nyud.net, to use the CoralCDN.
I have a fully qualified URI fullUri. How can I, in effect, do this:
fullUri.Host = fullUri.Host + ".nyud.net";
This needs to work for almost any URL, and the PORT of the request needs to be maintained.
Any help would be much appreciated.
You can use an UriBuilder to modify individual parts of an Uri:
Uri uri = new Uri("http://stackoverflow.com/questions/2163191/");
UriBuilder builder = new UriBuilder(uri);
builder.Host += ".nyud.net";
Uri result = builder.Uri;
// result is "http://stackoverflow.com.nyud.net/questions/2163191/"

Combining URIs and Paths

I am retro-fitting an application to make use of a PHP HTTP proxy (for caching) instead of the actual API server, the application currently combines the server URI and the path with the code:
methodUri = new Uri(apiUri, method.Path)
Where:
apiUri = "http://api.eve-online.com/" (System.Uri Object)
method.Path = "/char/SkillIntraining.xml.aspx" (string)
The result of the above statement is
"http://api.eve-online.com/char/SkillIntraining.xml.aspx" (System.Uri Object)
To use the PHP HTTP proxy the request would have to be changed as follows
apiUri = "http://www.r-s.co.uk/eproxy.php" (System.Uri Object)
method.Path = "/char/SkillIntraining.xml.aspx" (string)
The output I was expecting was:
"http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx" (System.Uri Object)
However the output I get is:
"http://www.r-s.co.uk/char/SkillIntraining.xml.aspx" (System.Uri Object)
I understand that this is the correct functionality of the constructor Uri(Uri, string), my question is what would be a better function or constructor to use in its place to get the output I expect? I have tried removing the leading "/" in method.Path taking it from an absolute path to a relative path however that did not help.
NOTE: both solutions below do work, however System.UriBuilder provides a more robust mechanism for combining URI's and paths and in my case resulted in fewer changes to resources than using System.Uri. Had I the choice I would mark both answers as correct.
Don't use the Uri object, use a UriBuilder - it copes way better with missing slashes
So
Uri apiUri = new Uri("http://www.r-s.co.uk/eproxy.php");
string methodPath = "/char/SkillIntraining.xml.aspx";
System.UriBuilder uriBuilder = new System.UriBuilder(apiUri);
uriBuilder.Path += methodPath;
Console.WriteLine(uriBuilder.Uri.ToString());
works as expected and produces http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx
Add a trailing "/" to apiUri, and remove the leading "/" from method.Path:
Uri apiUri = new Uri("http://www.r-s.co.uk/eproxy.php/");
string path = "char/SkillIntraining.xml.aspx";
Uri uri = new Uri(apiUri, path);
Console.WriteLine(uri.ToString());
Will print:
http://www.r-s.co.uk/eproxy.php/char/SkillIntraining.xml.aspx

Categories

Resources