Uri - append path [duplicate] - c#

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

Related

Cannot parse a correct URI

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");

.Net Webmaster API is incorrectly encoding URLs (Google.Apis.Webmasters.v3)

I've been struggling to get the new API to work. I am often getting this error when executing a query:
Error parsing NaN value. Path '', line 0, position 0.
From investigation I think the .Net code is incorrectly encoding URLs in the request by leaving slashes (/) unencoded. This changes the requests URL path and causes a 404.
If you omit the http:// part from the {site} part of a request it works. e.g. domain.com not http://domain.com/
There is no work around if your use an https site. Nor can you make any requests that require you to pass a specific URL outside the home page, as it will need to include a slash (/).
This is how I have done it in Google Geocode API. Not sure if that will help you but hope you get the idea.
public static string Sign(string url, string keyString)
{
ASCIIEncoding encoding = new ASCIIEncoding();
// converting key to bytes will throw an exception, need to replace '-' and '_' characters first.
string usablePrivateKey = keyString.Replace("-", "+").Replace("_", "/");
byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);
Uri uri = new Uri(url);
byte[] encodedPathAndQueryBytes = encoding.GetBytes(uri.LocalPath + uri.Query);
// compute the hash
HMACSHA1 algorithm = new HMACSHA1(privateKeyBytes);
byte[] hash = algorithm.ComputeHash(encodedPathAndQueryBytes);
// convert the bytes to string and make url-safe by replacing '+' and '/' characters
string signature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");
// Add the signature to the existing URI.
return uri.Scheme + "://" + uri.Host + uri.LocalPath + uri.Query + "&signature=" + signature;
}
This issue is being tracked here:
https://github.com/google/google-api-dotnet-client/issues/534
It relates to using .Net 4.0, and can be fixed by upgrading to .Net 4.5

How do I stop System.Uri from unencoding a 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

.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