niao
niao

Reputation: 5070

Webservice and encoding

I connect to the web service as follows:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(
    "http://mywebserviceaddress.com/attributes=someatt");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";

using (StreamReader stIn = new StreamReader(
       req.GetResponse().GetResponseStream(),Encoding.UTF8))
{
      strResponse = stIn.ReadToEnd();
      return strResponse;
}

However I get the response with (probably) bad encoding so as a result, on my page i get the following issue:

encoding failure

Am I doing something wrong or is it a third-party web service issue? How can I get the respone without this silly issues? Here's the screenshot from debugger:

enter image description here

Upvotes: 0

Views: 3611

Answers (1)

Yogu
Yogu

Reputation: 9445

The page is probably not UTF8. It looks like special characters use the upper half of an ASCII character, so it's some kind of old charset. Reading it as UTF8 causes errors because the reader doesn't expect single-byte special characters.

Store the result of GetResponse() into a variable and output the contents of ContentTypeCharacterSet. If the server acts correctly, it shows the used charset in this property. Then, you can use the correct charset in your StreamReader.

Upvotes: 1

Related Questions