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/"
Related
I have this link: www.axams-freizeitzentrum.com/ruifach-stadion.htm
when I try to create an Uri using this code:
var link = new Uri("www.axams-freizeitzentrum.com/ruifach-stadion.htm");
this will return
Invalid URI: The format of the URI could not be determined.
what is wrong?
Check possible reasons here: http://msdn.microsoft.com/en-us/library/z6c2z492(v=VS.100).aspx
You need to put the protocol prefix in front the address, i.e. in your case "http://"
var link = new Uri("http://www.axams-freizeitzentrum.com/ruifach-stadion.htm");
Uris need a scheme name.
var link = new Uri("http://www.axams-freizeitzentrum.com/ruifach-stadion.htm");
I try to build the following uri
http://localhost:8080/TestService.svc/RunTest
I do it as following
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost:8080/TestService.svc";
uriBuilder.Path = String.Format("/{0}", "RunTest");
string address = uriBuilder.ToString()
//In debugger the address looks like http://[http://localhost:8080/TestService.svc]/RunTest
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(address);
The above generates an exception
Invalid URI: The hostname could not be parsed.
I`ll appreciate your help in solving the issue
When using the Uri builder you need to put the host, port & path as it's own line. Also TestService.svc is also part of the path and not the host, you can get away with it if not using port but with port they have to be separated out.
var uriBuilder = new UriBuilder();
uriBuilder.Host = "localhost";
uriBuilder.Port = 8080;
uriBuilder.Path = String.Format("/{0}/{1}", "TestService.svc", "RunTest");
var address = uriBuilder.ToString();
When I run your code, I also see square brackets as the value for the address variable as you point out, but I don't see PerfTestService in the generated Uri, nor do I see why this would be?! I see:
http://[localhost:8080/TestService.svc]/RunTest
Since you already know the host and the path, I suggest you construct it as a string.
var uriBuilder = new UriBuilder("http://localhost:8080/TestService.svc/RunTest");
string address = uriBuilder.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
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));
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
I need to combine two urls, but it seems that UriBuilder doesn't support url's with ../../ in them. Is my only option to code this by hand? I'm trying something like this :
Uri pageUri = new Uri("http://site.com/a/b/c.html");
string redirectUrl = "../../x.html";
UriBuilder builder = new UriBuilder(pageUri);
builder.Path += redirectUrl;
Thanks for any tips on how to do this the right way.
You could also to use:
Uri redirect = new Uri(
new Uri("http://site.com/a/b/c.html"), "../../x.html");
This is working fine for me. You tried to call builder.Uri.OriginalString to get the full address back?