views:

1507

answers:

7

Hello Everybody

Here is my problem. I have a website in ASP.NET / C# which receives some data via GET/POST

This is "user filled" data, but not through a web page, it's a software that contacts my server.

Problem is, this software is sending data encoded in ISO-8859-1 (so Café would be sent as Caf%e9 ) and the rest of my SW/DB is Unicode

Also the data gets completely mangled, making recovery of what has been sent impossible :/

What would be the best way to deal with this?

I tried setting Request.ContentEncoding (before reading), but no avail.

A: 

How are you reading the data? Can you not read it as binary data and then perform the decoding yourself?

Jon Skeet
I'm reading using the NameValueCollection. I could try looking into BinaryRead.
+1  A: 

%e9 is just é but UrlEncoded. Server.UrlDecode your request string.

tsilb
I'm already calling UrlDecode, that's not the problem.%e9 is 'é' in ISO-8859-1 but then UrlDecode converts it to a UTF-8 string and everything gets mangled.
+1  A: 

Look at

http://msdn.microsoft.com/en-us/library/39d1w2xf.aspx

Short:

In the web.config write

<configuration>
  <system.web>
    <globalization
      fileEncoding="utf-8"
      requestEncoding="utf-8"
      responseEncoding="utf-8"
      culture="en-US"
      uiCulture="de-DE"
    />
  </system.web>
</configuration>

Remove the encoding entries in the aspx headers.

If utf-8 not correct try utf-16

I hope this helps.

Georg
A: 

If I understand you correctly, you are pulling this information out of an HTTP request. I'm going to assume it is the HTTP request body that is in the encoding.

You can use System.Text.Encoding.GetEncoding(...) to retrieve an Encoding object for ISO-8859-1. Then call GetDecoder() on that encoding object, and use it to interpret the request body. Ideally you'd determine the encoding type you load from Encoding.GetEncoding(...) from header values in the request, so servers with different configurations are supported.

Frank Schwieterman
A: 

Original author of the question here.

What helped me was Georg suggestion of setting Web.config variables, I put

requestEncoding="iso-8859-1"

And everything works now, thanks!

A: 

www.eng-fr.com

+1  A: 

The only thing that worked here was adding the following code to web.config:

<configuration>
  <system.web>
    <globalization requestEncoding="iso-8859-1"/>
  </system.web>
</configuration>

And then use

Request["varName"]

Do not use HttpUtility.UrlDecode or HttpUtility.UrlEncode, those 2 only work on the raw query string. Request[] already does the decode for you.

Thanks to JamesP for posting the idea himself.

Dorus