Is there a command in C# to convert strings like : https%3A%2F%2Fwww.google.com back to https://www.google.com?
some sort of "decryption" method maybe?
You need to use System.Web.HttpUtility.UrlDecode for this:
string real = System.Web.HttpUtility.UrlDecode(encodedString);
You can use the reverse function System.Web.HttpUtility.UrlEncode to encode.
This is not a matter of encryption or decryption. It is just that some characters cannot be expressed as part of parameters or other in a URL. For instance, a colon (:) cannot be part of a URL tail because it is used in the prefix (http:), so it gets encoded as %3A.
In the same way, a slash gets encoded as %2F. Hence, %3A%2F2%F means ://.
You can use HttpUtility.UrlDecode
You can try
HttpUtility.UrlDecode(url);
or
Uri.UnescapeDataString(url);
If you're not working on a web application, I suggest you use the WebUtility class instead as you don't have to import the entire System.Web assembly to access UrlDecode, which is required for the HttpUtility class. (You'll need to be targeting .NET 4)
string unencoded = WebUtility.UrlDecode("https%3A%2F%2Fwww.google.com");
You can also use Uri.UnescapeDataString if don't require any other HTML encoding/decoding methods. This is System.Uri so you don't need to import any other assembly.
Related
In my application I want to add sorting like that: 'https://example.com/entity?sort_by=+property'.
When I try to pass plus sign to query, ASP.NET Core returns 'property' without plus sign.
How can I fix this?
The plus symbol is a reserved character according RFC3986 which is the standard specification for URIs and it is used to represent a space, therefore, if you want to use it in your URL as literally the "+" character instead of a space, you need to encode it so it looks like:
https://example.com/entity?sort_by=%2Bproperty
In ASP.NET Core you can encode / decode URL's as follows:
using System.Net;
string url = "https://example.com/entity?sort_by=+property";
string encodedUrl = WebUtility.UrlEncode(url);
string decodedUrl = WebUtility.UrlEncode(encodedUrl);
See here for more info about URL encoding.
We ran into encoding issues with plus signs in url parameters and the fix for us was to use the following.
Uri.EscapeDataString(urlParameter);
Uri.UnescapeDataString(escapedUrlParameter);
Wanted to share this in case anyone else runs into the same issue.
How to encode URLs containing Unicode? I would like to pass it to a command line utility and I need to encode it first.
Example: http://zh.wikipedia.org/wiki/白雜訊
becomes http://zh.wikipedia.org/wiki/%E7%99%BD%E9%9B%9C%E8%A8%8A.
You can use the HttpUtility.UrlPathEncode method in the System.Web assembly (requires the full .NET Framework 4 profile):
var encoded = HttpUtility.UrlPathEncode("http://zh.wikipedia.org/wiki/白雜訊");
According to MSDN you can't use UrlPathEncode anymore.
So, Correct way of doing it now is,
var urlString = Uri.EscapeUriString("http://zh.wikipedia.org/wiki/白雜訊");
I had Turkish character problem.<a href="/#Html.Raw(string)" solved the problem
Server.UrlEncode(s);
.NET strings are natively Unicode strings (UTF-8 encoded, to be specific) so you need to nothing more than invoke HttpServerUtility.UrlEncode (though the so-called "intrinsic" Server property will be available in most contexts in asp.net where you may want to do this).
I am using silverlight / ASP .NET and C#. What if I want to do this from silverlight for instance,
// I have left out the quotes to show you literally what the characters
// are that I want to use
string password = vtakyoj#"5
string encodedPassword = HttpUtility.UrlEncode(encryptedPassword, Encoding.UTF8);
// encoded password now = vtakyoj%23%225
URI uri = new URI("http://www.url.com/page.aspx#password=vtakyoj%23%225");
HttpPage.Window.Navigate(uri);
If I debug and look at the value of uri it shows up as this (we are still inside the silverlight app),
http://www.url.com?password=vtakyoj%23"5
So the %22 has become a quote for some reason.
If I then debug inside the page.aspx code (which of course is ASP .NET) the value of Request["password"] is actually this,
vtakyoj#"5
Which is the original value. How does that work? I would have thought that I would have to go,
HttpUtility.UrlDecode(Request["password"], Encoding.UTF8)
To get the original value.
Hope this makes sense?
Thanks.
First lets start with the UTF8 business. Esentially in this case there isn't any. When a string contains characters with in the standard ASCII character range (as your password does) a UTF8 encoding of that string is identical to a single byte ASCII string.
You start with this:-
vtakyoj#"5
The HttpUtility.UrlEncode somewhat aggressively encodes it to:-
vtakyoj%23%225
Its encoded the # and " however only # has special meaning in a URL. Hence when you view string value of the Uri object in Silverlight you see:-
vtakyoj%23"5
Edit (answering supplementary questions)
How does it know to decode it?
All data in a url must be properly encoded thats part of its being valid Url. Hence the webserver can rightly assume that all data in the query string has been appropriately encoded.
What if I had a real string which had %23 in it?
The correct encoding for "%23" would be "%3723" where %37 is %
Is that a documented feature of Request["Password"] that it decodes it?
Well I dunno, you'd have check the documentation I guess. BTW use Request.QueryString["Password"] the presence of this same indexer directly on Request was for the convenience of porting classic ASP to .NET. It doesn't make any real difference but its better for clarity since its easier to make the distinction between QueryString values and Form values.
if I don't use UFT8 the characters are being filtered out.
Aare you sure that non-ASCII characters may be present in the password? Can you provide an example you current example does not need encoding with UTF-8?
If Request["password"] is to work, you need "http://url.com?password=" + HttpUtility.UrlEncode("abc%$^##"). I.e. you need ? to separate the hostname.
Also the # syntax is username:password#hostname, but it has been disabled in IE7 and above IIRC.
I'm trying to call a rest webservice provided by a lims system (basically a chemistry lab database + interface). It was working great until some > ascii characters showed up (specifically characters with circumflexes, umlauts, etc.)
When calling the webservice passing the value àèïõû I have the following argument:
&componentValue=àèïõû
HttpWebRequest, without any pre-escaping OR with Uri.EscapeDataString() called on the value gives:
à èïõû
Firefox, with the same website as was passed to HttpWebRequest gives the correct value:
àèïõû
Now for the escaping itself:
Uri.EscapeDataString() appears to escape "àèïõû" as:
%C3%A0%C3%A8%C3%AF%C3%B5%C3%BB
Firefox escapes "àèïõû" as:
%E0%E8%EF%F5%FB
As the latter works I would of course prefer to use that as my escape method, but I really don't know where to begin. I've found plenty of information on different methods of handling encodes on the response data, but not on the request.
From MSDN:
Uri.EscapeDataString Method
[...] All Unicode characters are converted to UTF-8 format before being escaped.
So what you're seeing is the UTF-8 encoded version of àèïõû.
Unlike Uri.EscapeDataString, HttpUtility.UrlEncode allows you to specify an encoding explicitly:
HttpUtility.UrlEncode("àèïõû", Encoding.GetEncoding("latin1"));
Alternatively, you could write your own version; for example:
string.Concat(Encoding
.GetEncoding("latin1")
.GetBytes("àèïõû")
.Select(x => "%" + x.ToString("x2"))
.ToArray());
Both result in "%e0%e8%ef%f5%fb".
A better solution would probably be to accept UTF-8 encoded query strings in the webservice.
It appears that Uri.HexEscape() will do what you want, but only one character at a time. I'd roll your own escaping function and hope that your codepage is always the same codepage that the webservice is using, since it appears that the webservice doesn't support Unicode.
what c# class method can I use to URL encode a URL string?
In my use case I want to pass a URL string as a URL parameter itself. So like burying a URL within a URL. Without some encoding the "&" and "?" characters in the inner URL can get picked up when the parameters for the outer Url parameters are processed
thanks
HttpUtility.UrlEncode
System.Net.WebUtility.UrlEncode(str)
HttpServerUtility.UrlEncode might be exactly what you are looking for.
Use Server.UrlEncode
You need an instance of the HttpServerUtility class, because the UrlEncode method is not static.
See http://msdn.microsoft.com/en-us/library/system.web.httpserverutility(v=VS.90).aspx
I am operating with .net framework 2.0. But it never shows me "UrlEncode" intelisense when I place dot after httpserverutility. Here are the options I see:
HttpServerUtility.Equals
HttpServerUtility.ReferenceEquals
HttpServerUtility.UrlTokenDecode
HttpServerUtility.UrlTokenEncode
That's it, these are the only options I see. Could I possibly ask for small piece of code as an example to use url encode function?
Thanks,
Linda