JamesP
JamesP

Reputation:

C# HttpRequest and different encodings

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.

Upvotes: 3

Views: 6489

Answers (5)

Georg
Georg

Reputation: 90

Look at

How to: Select an Encoding for ASP.NET Web Page Globalization

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.

Upvotes: 1

Dorus
Dorus

Reputation: 7546

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.

Upvotes: 3

JamesP
JamesP

Reputation:

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!

Upvotes: 1

Frank Schwieterman
Frank Schwieterman

Reputation: 24480

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.

Upvotes: 0

tsilb
tsilb

Reputation: 8037

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

Upvotes: 1

Related Questions