How to decode encoded URL in google chrome? - c#

Google chrome automatically converts unicode strings in URL to something like this;
?querystring=مقالات
?querystring=%D9%85%D9%82%D8%A7%D9%84%D8%A7%D8%AA
My question is how to decode the encoded text in the codes for example for a comparing purpose?
if (Request.Url.Query == "?querystring=مقالات")
//do something

Check these out :)
Console.WriteLine(System.Web.HttpUtility.UrlDecode("http://www.google.com/search?q=مقالات"));
Console.WriteLine(System.Web.HttpUtility.UrlEncode("http://www.google.com/search?q=%D9%85%D9%82%D8%A7%D9%84%D8%A7%D8%AA"));

URL encoding ensures that all browsers will correctly transmit text in URL strings.
Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers. As a result, these characters must be encoded in tags or in query strings where the strings can be re-sent by a browser in a request string.
UrlDecode is a convenient way to access the HttpUtility.UrlDecode method at run time from an ASP.NET application. Internally, UrlDecode uses HttpUtility.UrlDecode to decode strings.
The following example decodes the string named EncodedString (received in a URL) into the string named DecodedString :
String DecodedString = Server.UrlDecode(EncodedString);

Related

Why is UTF-7 interpreting umlauts correct and UTF-8 not?

I have a Base64 string which I want to convert and decode to UTF-8 like this:
byte[] encodedDataAsBytes = System.Convert.FromBase64String(vcard);
return Encoding.UTF8.GetString(encodedDataAsBytes);
This because Umlauts in the string need to be displayed correctly. The problem I face is that when I use UTF-8 as encoding the umlauts are NOT handled correctly. But when I use UTF-7
return Encoding.UTF7.GetString(encodedDataAsBytes);
everything works fine.
Why's that? Should'nt UTF-8 be able to handle umlauts??
Your vcard is UTF-7 encoded.
This is why Encoding.UTF7.GetString(encodedDataAsBytes); gives you the right result.
After it is encoded, you can't decide on another encoding.
To use UTF-8 encoding you would need access to the string before variable vcard got its value.
I had a similar problem. In my case, I used javaScript btoa() to encode a filename to Base64 within the Web UI, and send it over to the server. On the server side .net core, I used the code below to decode it back to a string filename.
// Note: encodedFilename is the result of btoa() from the client web UI.
var raw = Convert.FromBase64String(encodedFilename);
var filename = Encoding.UTF8.GetString(raw);
It failed to decode ä. However it worked when I used Encoding.UTF7(), but I think it is not the right solution. I believe that this due to the different encode/decode type. btoa() is binary to ASCII. What I really need is b64EncodeUnicode().
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
Code Reference: https://developer.mozilla.org/en-US/docs/Glossary/Base64

Request.QueryString not returning the correct value

I'm using AES encryption for the values of a URL. I've sampled it here with only one parameter to demonstrate the problem:
http://localhost:12345/pagename?id=ha3bEv8A%2ffs0goPGeO6NPQ%3d%3d
Request.QueryString["id"] returns "ha3bev8a/fs0gopgeo6npq==" which clearly does not match the value of the encrypted ID. Is something tripping up QueryString?
You are getting a URL-encoded query string, which Request.QueryString["id"] seems to decode for you. You could always just re-encode it:
string decodedId = Request.QueryString["id"];
string reEncodedId = HttpUtility.UrlEncode(decodedId);
The value you are seeing is in fact correct. What might be confusing you, is in the way it is presented. The id value in the URL is encoded in URL Encoding.
Some characters have to be encoded in the URL String in a different way, as they are special characters that sometimes can mess up the way the string is interpreted if they aren't encoded properly.
For example, in the query string you provided:
http://localhost:12345/pagename?id=ha3bEv8A%2ffs0goPGeO6NPQ%3d%3d
The %2f characters are a way to encode the '/' character, while %3d is a way to encode the '=' character.
When you get the value by getting Request.QueryString["id"] , it is decoding it back from an URL Encoded string to raw text.
Check this page for more reference.
https://www.w3schools.com/tags/ref_urlencode.asp

Urlencode large amount of text in .net 4 client C#

What's the best way to urlencode (escape) a large string (50k - 200k characters) in the .net 4 client profile?
System.Net.Uri.EscapeDataString() is limited to 32766 characters.
HttpUtility.UrlEncode is not available in .net 4 client.
The encoded string is to be passed as the value of a parameter in an httprequest as a post.
(Also, is there a .net-4-client profile tag on SO?)
Because a url encoded string is just encoded character by character it means that if you split a string and encode the two parts then you can concatenate them to get the encoded version of the original string.
So simply loop through and urlencode 30,000 characters at a time and then join all those parts together to get your encoded string.
I will echo the sentiments of others that you might be better off with a content-type of multipart/form-data. http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4 explains the differences in case you are unaware. Which of these two you choose should make little difference to the destination since both should be fully understood by the target.
I would suggest looking in to using a MIME format for posting your data. No need to encode (other than maybe a base64 encoding) and would keep you under the limitation.
You could manually encode it all using StringBuilder, though it will increase your transfer amount threefold:
string EncodePostData(byte[] data)
{
var sbData = new StringBuilder();
foreach(byte b in data)
{
sbData.AppendFormat("%{0:x2}", b);
}
return sbData.ToString();
}
The standard method, however, is just to supply a MIME type and Content-Length header, then send the data raw.

Can I add a string converted to Base64 as part of a URL?

I have a string converted with encryption. I would like to make this part of a URL. Is that possible if it has been converted to base64 or do I need to do something more?
var going_to_be_part_of_url = System.Convert.ToBase64String(bytOut, 0, i);
Thanks
Yes, but it's not a good idea, Base64 requires that you respect the difference between upper case and lower case. URL's aren't typically case strict.
Then there's the problem of the special characters in Base64 being converted to URL encoded equivalents, making your URL's ugly and less manageable.
You should go with Base36 instead.
You can use a modified base 64 for URL Applications which is just base64 with a couple of the problem characters replaced.
The easiest way is to take your base64 string and encode perform a string replace on the problem characters when building the URL, and reversing the process when interpreting the URL.

Url encoding quotes and spaces

I have some query text that is being encoded with JavaScript, but I've encountered a use case where I might have to encode the same text on the server side, and the encoding that's happening is not the same. I need it to be the same. Here's an example.
I enter "I like food" into the search box and hit the search button. JavaScript encodes this as %22I%20like%20food%22
Let's say I get the same value as a string on a request object on the server side. It will look like this: "\"I like food\""
When I use HttpUtility.UrlEncode(value), the result is "%22I+like+food%22". If I use HttpUtility.UrlPathEncode(value), the result is "\"I%20like%20food\""
So UrlEncode is encoding my quotes but is using the + character for spaces. UrlPathEncode is encoding my spaces but is not encoding my escaped quotes.
I really need it to do both, otherwise the Search code completely borks on me (and I have no control over the search code).
Tips?
UrlPathEncode doesn't escape " because they don't need to be escaped in path components.
Uri.EscapeDataString should do what you want.
There are a few options available to you, the fastest might be to use UrlEncode then do a string.replace to swap the + characters with %20.
Something like
HttpUtility.UrlEncode(input).Replace("+", "%20");
WebUtility.UrlEncode(str)
Will encode all characters that need encoded using the %XX format, including space.

Categories

Resources