URI encodes control signs into absolute path - c#

We are sending data to an API with several endpoints. For this we put the base URL into a variable and appending the route info to it. But the request cannot be resolved to a service, as the URI-object is putting not printable characters into the path.
The code for create the URI object:
var uri = new Uri(_url + "/api/v1/create");
The result is:
https://localhost%E2%80%8B/api%E2%80%8B/v1%E2%80%8B/create
We are using .Net Framework 4.7.2.
Does anyone know, whats happening?

As canton7 pointed out, the problem had been the zero-length space here
We retyped the whole method calls - not only the URLs - and now the system is running. Thanks.

Related

Correct way of manipulating the url coming from App.config

I have a URL ("http://localhost:2477/") on which I do get and post request. I have stored this URL in the app.config file of my project.
In the code, depending on the function, I add the string "getValue?id={0}" or "postValue" to this URL. But I later ran into an issue when I changed the URL to "http://localhost:2477" (no forward slash in the end) in the app.config.
Took me some embarrassing amount of time to figure out this issue, which made me wonder if there is a good way to handle this case.
Irrespective of the case when there is a forward slash or not in the URL, I want my code to change it to a proper URL.
Always use Path.Combine(string, string). This method will conform a valid path and should add the / if needed.
edit
I realized my answer does not work for URL, just for file paths.
What you’re looking for is Uri constructor instead.
Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri, "catalog/shownew.htm");
Using the Uri class you can modify your URL more elegantly. You can access the Host, Port, Query, etc. with ease. A similar question was asked here.
Try to use the UriBuilder, it's far more flexible as the Uri Constructor.
See https://stackoverflow.com/a/20164328/10574963

ASP.NET: parse url having # (hash) sign

I need to parse url that has something after # (hash) sign in my asp.net application. How to do it easily?
Thank you,
You're looking for the Uri class:
new Uri(someString).Fragment
Note that the hash is not sent to the server in an HTTP request.
url.Substring(url.IndexOf('#') + 1)
...where "url" is a string containing the url in question
This is called "hash sign" URI.
After client gets PAGE responsed including js,
the contents after '#' would be handled by client using responsed js to get "real" URL for redirection.
SEE: https://www.w3.org/2001/tag/2011/01/HashInURI-20110115#References
Omg, I meant fragment (after #) part on the server... Though I've looked thrugh and found that it seems to be impossible......

WebBrowser keep url/uri encoded dont decode

I have a web-browser in a win form application and I am experiencing issues when opening a URL.
The URL I pass in as a new URL instance is encoded with:
/ as %2f , ? as %3f and the
= as %3d
But when I debug my code I can see that the absolute URL or any of the other ones in the webbrowser.url.* is decoded as / , ? and =.
How do I keep the URL encoded? The URL will not work if It is not encoded like that.
I found a solution to my problem, when you have a URL that looks something like this:
domain.com/action/doaction/?identity=12354698789
And you want it encoded like this:
domain.com/logon?returnurl=action%2fdoaction%2f%3fidentity%3d12354698789
That does not work in your web browser. It decodes it to the first url.
I needed the id in the doaction controller so I used this code:
string orgId = ControllerContext.RouteData.Values["id"].ToString();
It returns that url, if unsure, debug and trace through, you will find the right key and value.
Why is it a problem?
If you want the undecoded URL, use the HttpRequest.RawUrl Property. The query string is automatically decoded by default and there is no public parameter that would turn it off.

301 Redirect with unicode characters - C#

I need to do a 301 redirect on a URL that may have Unicode characters in it.
HttpUtility.UrlEncode isn't doing what I need because if I encode the whole URL it encodes any ':' or '/'
HttpUtility.UrlEncode("http://www.हिन्दी.com") = http%3a%2f%2fwww.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com
(also: http://www.%e0%a4%b9%e0%a4%bf%e0%a4%a8%e0%a5%8d%e0%a4%a6%e0%a5%80.com doesn't seem to work in firefox or IE, but it does in Chrome)
Only other thing I can think of is to encode the different parts of the URL so that the protocol doesn't get encoded.
You need to take a look at RFC 3490 which details how to correctly encode international domain names -- this is also why when you encode just the domain portion it only works in Chrome)
So I figured out a almost 100% solution to this. Thanks to Rowland Shaw and Rup for pointing me in the direction of IDNs.
I tried using an IdnMapper, whose function GetAscii will convert unicode domain names to punycode, but I didn't have the domain separated from the rest of the URL. I tried putting the url into a Uri object, but I would get a UriFormatException if the url had unicode characters.
That led me to: http://msdn.microsoft.com/en-us/library/system.uri(v=VS.90).aspx
which tells how to enable the Uri class to accept unicode and do the IDN and IRI conversions. It says you have to add something to the .NET 2.0 machine.config file, but you can put the line in web.config and it will work.
After I got the Uri working with unicode, I pieced together the url and did a redirect:
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", uri.Scheme + "://" + uri.DnsSafeHost + uri.PathAndQuery + uri.Fragment);
Response.End();
This works for Chrome and Firefox 3.6, but fails in IE8. I'm still trying to solve that problem and will update here if I find a solution.

Why doesn't FTPWebRequest, or WebRequest in general accept a /../ path?

I am trying to automate some upload/download tasks from an ftp web server. When I connect to the server through client, or through Firefox even, in order to get to my directory, I have to specify a path like this:
ftp://ftpserver.com/../AB00000/incoming/files
If I try to access this:
ftp://ftpserver.com/AB00000/incoming/files
The server throws an error that the directory does not exist. So, the problem:
I am trying to create an FTPWebRequest with the first ftp address, but it always parses out the "/../" part and then my server says the path doesn't exist.
I've tried these:
Uri target = new Uri("ftp://ftpserver.com/../AB00000/incoming/files");
FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);
and
string target = "ftp://ftpserver.com/../AB00000/incoming/files";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
In the first bit, the path is already incorrect when the Uri object is instantiated, in the second bit, it's after the WebRequest.Create method. Any ideas what's going on?
EDIT:
Additionally, since I posted this, I have tried creating the URI with the no parse option. I have also tried something like this:
string ftpserver = "ftp://ftpserver.com/../";
string path = "12345/01/01/file.toupload";
Uri = new Uri(ftpserver, path, true);
And it always parses out the root part ("/../").
Try escaping the .. with something like:
Uri target = new Uri("ftp://ftpserver.com/%2E%2E/AB00000/incoming/files");
That works according to this blog which I found in this discussion.
Not really sure about it, but it may be for security reasons, since allowing "/../" URIs would potentially let people navigate freely on any server's file system.
Also, the official URI RFC states that when resolving an URI one of the steps performed is actually the removal of "/../" segments, so it's not a problem in the C# library but it's regular URI behavior.
Have you tried using the # symbol like so?
Uri target = new Uri(#"ftp://ftpserver.com/../AB00000/incoming/files");
FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);

Categories