Wednesday, March 21, 2012

XML Web Services Encoding but No Soap Message

Hi,

I have double byte characters in the content that I am returning using Web Services. However, the encoding in the xml file returned by Web Services is utf-8 and I am unable to read the content, not even by changing browser encoding setting to the appropriate one.

I implemented SoapExtension to modify the xml encoding before it returns to the client, which works when a WinForm calls it, but it doesn't work when a webpage calls it, and here is why:
http://www.dotnet247.com/247reference/msgs/45/228371.aspx
"If you're testing this by hitting the "Invoke" button on the .asmx page, it will never work. That execution path does not send any SOAP message to the ASP.NET runtime, and no SOAP Extension will ever get executed. You need to generate a client and use it to send some traffic at the service if you want to test your extension."

Then I created an aspx page to call my web service using Atlas, stepping through in debug mode, the code did not touch the SoapExntension at all. So Atlas doesn't get SoapMessage, therefore using SoapExtension to modify xml doesn't work?

How do I modify the xml that the Web Services send to me in this case? HttpHandler?

Thanks,
Janh

I forgot to mention that the data is stored in sql server as windows-1252 (or iso-8859-1), not nvarchar. If user inserts big5, it'll store big5 in windows-1252 format.

When I return the string to the client, I see ¤é?g?ü???Q|?|ü. The problem is, that no matter what browser encoding setting I change to, I always see this string. I don't know why but it should change to 日經指數昨收至 when I change browser encoding to big5.

I found something that kind of works. Since I know it is big5, I am able to do:

System.Text.Encoding iso = System.Text.Encoding.GetEncoding(1252);
System.Text.Encoding iso2 = System.Text.Encoding.GetEncoding(950); //code page for big5
byte[] isoBytes = iso.GetBytes(content); //content is the string from the database
newcontent = iso2.GetString(isoBytes);

send it via web service and Atlas to the client. It displays the string automatically and correctly in big5 encoding. But when I change the browser encoding setting to anything else, it is still stuck as the same big5 string. It is somehow fixed, unchangeable.

This behavior is the same if, instead of using Atlas and web service, I just createnew XMLHttpRequest()andxmlhttp.open("GET", "test.aspx",true) which the aspx page returns the same string as above. The string still has a fixed encoding regardless of browser encoding setting.

Is the problem in how XMLHttp is handling the string? That makes the string encoding unchangeable on the client side?

This is bad for me because I have no way of knowing what encoding to use. With this example, I converted the encoding to big5 because I know it's big5 in the database. I have many other languages in my database.

I would still like to use Atlas... but is there a way to NOT add any manual encoding at all, return the raw string to the client, and let client change the browser setting to view the correct encoding?


Sorry guys, I found my own answer.

Guess responseText is ALWAYS utf-8, so I am supposed to use responseXML in order to preserve the encoding.

So my new question is, how do I use responseXML in Atlas...? Or is there another way to preserve encoding?


How do we solve this? please?Big Smile

There is a way to tell Atlas to treat the response as XML instead of text, for webservices that you want to return XML instead of JSON, you do this thru the WebOperationAttribute on the asmx webmethod...

[WebOperation(true /*getVerbEnabled*/, ResponseFormatMode.Xml, false /*safeForCrossDomain*/)]

Hope that helps,
-Hao

No comments:

Post a Comment