C# - Convert URL to plain text

Bach Nguyen
Bach Nguyen used Ask the Experts™
on
Hi,

I am trying to convert an URL format to plain text format by this way:

 string fileTxt = File.ReadAllText("test1.txt");

  MessageBox.Show(Uri.UnescapeDataString(fileTxt)); --> It doesn't work, it shows: oJ\x2BkTRbYHjeME72JTd\x2F\x2Fnb\x2BKt0RWeAmk8Gzw7dOc\x2FAM\x3D:1438850410112:hEj6agVToFMpYj7MufF9Qg\x3D\x3D

The content of "test1.txt": oJ\x2BkTRbYHjeME72JTd\x2F\x2Fnb\x2BKt0RWeAmk8Gzw7dOc\x2FAM\x3D:1438850410112:hEj6agVToFMpYj7MufF9Qg\x3D\x3D

If I use directly the string: MessageBox.Show(Uri.UnescapeDataString("oJ\x2BkTRbYHjeME72JTd\x2F\x2Fnb\x2BKt0RWeAmk8Gzw7dOc\x2FAM\x3D:1438850410112:hEj6agVToFMpYj7MufF9Qg\x3D\x3D")); ---> It works.


Could you let me know how to make string fileTxt = File.ReadAllText("test1.txt"); work?

Thank you!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
When you use a string literal like that in C# the special characters are already processed in the code before any functions are called. So the unescape does nothing in that case. If you leave it out you get the same result:
MessageBox.Show("oJ\x2BkTRbYHjeME72JTd\x2F\x2Fnb\x2BKt0RWeAmk8Gzw7dOc\x2FAM\x3D:1438850410112:hEj6agVToFMpYj7MufF9Qg\x3D\x3D"); // ---> It "works".

Open in new window


However, I suspect this is actually not the correct string. Look at it carefully below at *1*: there is a strange character that I don't "trust" (just before M=:).

How is the file generated? It does not look like it's generated with Uri.EscapeDataString() because then you would see %xx tokens in the string. In fact you can use the unescape if you replace "\x" with "%" in fileTxt:
MessageBox.Show(Uri.UnescapeDataString(fileTxt.Replace(@"\x", "%")));

Open in new window


This gives a slightly different result (see *2* below) because of \x2FA which is processed as a double byte character in the literal string but probably needs to be processed as \x2F (a slash) followed by "AM" etc.

the 2 outputs:
*1* oJ+kTRbYHjeME72JTd//nb+Kt0RWeAmk8Gzw7dOc˺M=:1438850410112:hEj6agVToFMpYj7MufF9Qg==
*2* oJ+kTRbYHjeME72JTd//nb+Kt0RWeAmk8Gzw7dOc/AM=:1438850410112:hEj6agVToFMpYj7MufF9Qg==

Open in new window


Be careful because it seems there's more going on: the file may contain 3 fields separated by colons because if it was 1 string I would expect they would have been encoded to \x3A. You may need to split it up first because if any of the fields contain an encoded colon then a split afterwards would result in more than 3 fields.

Author

Commented:
Thank you very much!

I got it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial