HttpWebResponse shows junk characters in Headers C# - c#

Here is the code snippet that returns a persian language word in Response.AddHeaders
and here is a code snippert that get response via HttpWebResponse and shows junk characters for that persian word
I'm curious why does it returns these garbage characters? And how i can rectify this issue? On my aspx page the junk text appears like this-
Please help as soon as possible...
Thanks

i found unicode encoding useful.
pwd = HttpUtility.UrlEncodeUnicode(pwd); // encode string to unicode
pwd = HttpUtility.UrlDecode(pwd); // decode unicode to string

Related

How to keep Json.Encode/Decode from converting characters into chinese letters in C#?

so first off, i know next to nothing about encoding, so thats probably the problem.
What I'm trying to do is Decode and Encode a String with Json.Decode and Json.Encode. So in the end, there should be the same string as before.
So a part of my target string has the following values in it :
"\":\"\u6b22\u8fce\u6765\u5230\" etc you get the idea. So now i convert the String like this:
DynamicJsonObject jsonObject = Json.Decode(Raw);
String newRaw = Json.Encode(jsonObject);
Now newRaw should be the same as Raw, am i right? Well the characters i mentioned look like this in newRaw:
欢迎来到《奇异人生:暴风前夕》。要及时获得最新
How can i fix that? I know that Raw has UTF8 encoding, but converting newRaw to UTF8 did not help! Thanks in advance.
"\u6b22" is a string constant that contains a single character: the unicode codepoint 6b22, a simplified Chinese character. This isn't a question of Json, but a question of c# string escape sequences.

German character ß encoding in Livelink using C#

I have folder name that contains German special character such äÄéöÖüß.The following screenshot display contents of LiveLink server.
I want to extract folder from Livelink server using C#.
valueis obtained from LLserver.
var bytes = new List<byte>(value.Length);
foreach (var c in value)
{
bytes.Add((byte)c);
}
var result = Encoding.UTF8.GetString(bytes.ToArray());
Finally, the result is äÄéöÖü�x .where ß is seen as box character '�x'. All other characters present in folder name are decoded successfully/properly except the ß character.
I am just wondering why the same code works for all other German special characters but not for ß.
Could anybody help to fix this problem in C#?
Thanks in advance.
Go to admin panel of server Livelink/livelink.exe?func=admin.sysvars
and set Character Set: UTF-8
and code section change as follow
byte[] bytes = Encoding.Default.GetBytes(value);
var retValue = Encoding.UTF8.GetString(bytes);
It works fine.
You guessed your encoding to be UTF8 and it obviously is not. You will need to find out what encoding the byte stream really represents and use that instead. We cannot help you with that, you will have to ask the sender of said bytes.

Unicode literal string

I'm sending some JSON in an HTTP POST request. Some of the text within the JSON object is supposed to have superscripts.
If I create my string in C# like this:
string s = "here is my superscript: \u00B9";
... it converts the \u00B9 to the actual superscript 1, which breaks my JSON. I want the \u00B9 to show up exactly as I write it in the the string, not as a superscript.
If I add an escape character, then it shows up like:
"here is my superscript: \\u00B9"
I don't want to use an escape character, but I also don't want it to be converted to the actual superscript. Is there a way to have C# not do Unicode conversion and leave it as literally: "\u00B9"?
If I understand your question correctly... add the at symbol (#) before the string to avoid the escape sequences being processed
string s = #"here is my superscript: \u00B9";
http://msdn.microsoft.com/en-us/library/362314fe(v=vs.80).aspx
I like #NinjaNye's answer, but the other approach is to use a double-backslash to make it literal. Thus string s = "here is my superscript: \\u00B9"
is recommended you encode your string before send to server. You can encode using base64 or URLEncode in client and decode in server side.

Decoding Base64 / Quoted Printable encoded UTF8 string

In my ASP.Net application working process, I need to do some work with string, which equals something like
=?utf-8?B?SWhyZSBCZXN0ZWxsdW5nIC0gVmVyc2FuZGJlc3TDpHRpZ3VuZyAtIDExMDU4OTEyNDY=?=
How can I decode it to normal human language?
Thanks in advance!
Update:
Convert.FromBase64String() does not work for string, which equals
=?UTF-8?Q?Bestellbest=C3=A4tigung?=
I get The format of s is invalid. s contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters. exception.
Update:
Solution Here
Alternative solution
Update:
What kind of string encoding is that: Nweiß ???
It's actually a base-64 string:
string zz = "SWhyZSBCZXN0ZWxsdW5nIC0gVmVyc2FuZGJlc3TDpHRpZ3VuZyAtIDExMDU4OTEyNDY=";
byte[] dd = Convert.FromBase64String(zz);
// Returns Ihre Bestellung - Versandbestätigung - 1105891246
string yy = System.Text.Encoding.UTF8.GetString(dd);
I've written a library that will decode these sorts of strings. You can find it at http://github.com/jstedfast/MimeKit
Specifically, take a look at MimeKit.Utils.Rfc2047.DecodeText()
This seems to be MIME Header Encoding. The Q in your second example indicates that it is Quoted Printable.
This question seems to cover the variants fairly well. In a quick search I didn't find any .NET libraries to decode this automatically, but it shouldn't be hard to do manually if you need to.
That's not UTF8. Thats a Base64 encoded string.
the UTF-8 only indicates that the target string is in UTF8 format.
After decoding the Base64 string:
SWhyZSBCZXN0ZWxsdW5nIC0gVmVyc2FuZGJlc3TDpHRpZ3VuZyAtIDExMDU4OTEyNDY=
You'll get the following result:
Ihre Bestellung - Versandbestätigung - 1105891246
See Base64 online decode/encode
Looks like a base64 string.
Try Convert.FromBase64String
http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx
This is an encoded word, which is used in email headers when there is non-ASCII content. Encoded words are defined in RFC 2047:
https://www.rfc-editor.org/rfc/rfc2047#section-2
The BNF for an encoded word is:
encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
So the correct way to interpret this is:
The data is the stuff between the 3rd and 4th question marks
It has been Base64 encoded (the 'B' stands for Base64; if it were a
'Q' then it would be quoted-printable).
Once you decode the
data, it will be in the UTF-8 character set.
The result, as #Shai correctly pointed out, is:
Ihre Bestellung - Versandbestätigung - 1105891246
This is German. The umlaut is obviously the reason for the UTF-8 and thus the need for an encoded word. The translation is:
Your order - Delivery confirmation - 1105891246
Apparently it's a tracking number for an order.
All modern email clients (and Outlook) transparently support encoded words.
This is a bit of guesswork, but let's try
remove =? from start and ?= from end
keep the start up to the next ? as the character set
Remove the B? - don't know, what it is
Convert the rest to a byte[] via System.Convert.FromBase64String()
Convert this to the final String via Encoding.GetSTring() using the character set remembered in the second step

Extract Chinese text from Query string

I need to extract Chinese characters from the query string in an ASP.NET web application.
When I tried it, I get "????" instead of the actual text. I know I need to decode it with UTF-8 but its does not work. I have used:
String text = System.Web.HttpUtility.UrlDecode(Request.QueryString["text"], System.Text.Encoding.UTF8);
but I get "???" back from the operation.
Please help.
There are two cases.
1st Case where your URL is real in chinese, the only function that get it is the Request.RawUrl (and not the Request.QueryString["text"]) From Request.RawUrl you need manually get your Chinese text from text=ελληνικασανκινεζικα.
2nd Case where you have first Encode your URL string before you send it. In this case the code I use is
String text = Server.UrlDecode(Request.QueryString["text"]);
Hope this help.
Note: If you try to make test with Google Chrome, then what you type on url chrome is encode/decode automatically by browser and you are not see what actual you send. Try to use ie, for make your test.

Categories