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 (called EncodingExtension) to rewrite the xml to
change the encoding="utf-8" to encoding="windows-1252" in BeforeDeserialize
SoapMessage stage on the server side. I am able to see the modified xml when
viewing it with WebServiceStudio 2.0 (a WinForm app). but I am unable to get
the same change when using a web application, where I view it using simply
invoking the same webmehod from the browser (calling asmx page directly in
the browser).
Invoking it in WebServiceStudio, I am able to debug and step though the
SoapExtension class. But invoking webmethod directly in the browser, I am
unable to step through, as if the SoapExtension class isn't being called at
all, which explains why the changes to xml stream aren't applied. Though I
did get as far as to the EncodingExtensionAttribute class, ExtensionType
function. But it won't run the EncodingExtension class.
My SoapExntension class obvious works. So my questions is, what do I have
to do to get the SoapExtension to run when invoking Web Services from a web
application?
I am using .NET Framework 2.0, Visual Studio 2005 RTM. I want the XML file
to have windows-1252 encoding. This xml is sending it to my aspx page that
uses Atlas to call Web Services. Now I am wondering if this is even the
correct approach?
Please help! Below are some of my code setup and the result.
Thanks.
JaN
----------- SETUP -----------------:
WebService.asmx:
[EncodingExtension]
[WebMethod]
public Weblog GetWeblogDebug(int weblogid, int userId) { //some code }
XWebServices.EncodingExtension (inherit from SoapExtension):
private void AlterEncoding(SoapMessage message)
{
if (message.Stage == SoapMessageStage.AfterSerialize)
{
newStream.Position = 0;
TextReader reader3 = new StreamReader(newStream);
string str = reader3.ReadToEnd();
int pos = str.IndexOf("<GetWeblogDebugResponse");
int pos2 = str.IndexOf("</GetWeblogDebugResponse>");
string soapBodyString = str.Substring(pos, pos2 - pos + 25);
soapBodyString = soapBodyString.Replace("<Title>null</Title>",
"<Title>Hello</Title>");
soapBodyString = soapBodyString.Replace("<Title />", "<Title>Hello</Title>");
String xmlVersionString = "<?xml version=\"1.0\" encoding=\"big5\"?>";
String soapEnvelopeBeginString = "<soap:Envelope
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
String soapBodyBeginString = "<soap:Body>";
String soapBodyEndString = "</soap:Body>";
String soapEnvelopeEndString = "</soap:Envelope>";
Stream appOutputStream = new MemoryStream();
StreamWriter soapMessageWriter = new StreamWriter(appOutputStream);
soapMessageWriter.Write(xmlVersionString);
soapMessageWriter.Write(soapEnvelopeBeginString);
soapMessageWriter.Write(soapBodyBeginString);
soapMessageWriter.Write(soapBodyString);
soapMessageWriter.Write(soapBodyEndString);
soapMessageWriter.Write(soapEnvelopeEndString);
soapMessageWriter.Flush();
appOutputStream.Flush();
appOutputStream.Position = 0;
StreamReader reader = new StreamReader(appOutputStream,
System.Text.Encoding.GetEncoding(1252));
StreamWriter writer = new StreamWriter(oldStream,
System.Text.Encoding.GetEncoding(1252));
writer.Write(reader.ReadToEnd());
writer.Flush();
appOutputStream.Close();
}}
------------- RESULT -------------------:
The <Title> is what I manually changed (in additition to encoding), to see
if it actually works. Because for some reason WebServiceStudio always use
utf-16, but I am always seeing Title being changed to "Hello" as intended in
the code. In the browser generated xml, the title is not changed, and xml
encoding is always utf-8.
/WeblogEditorService.asmx/GetWeblogDebug Result:
<?xml version="1.0" encoding="utf-8" ?>
- <Weblog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<weblogChangeOnly>true</weblogChangeOnly>
<epropStatusChange>0</epropStatusChange>
<UserId>41</UserId>
<UniqueId>992</UniqueId>
<Title />
<Body>¤é¸g«ü¼Æ¬Q¦¬¦Ü15551.31¡A¦]§ë¸ê¤H¶R¶i¿ûÅKªÑ¡A³Ð³æ¤é¦¨¥ ¬ö¿ý·s°ª¡Cserverinsert</Body>
</Weblog>
WebServiceStudio 2.0 Result:
ResponseCode: 200 (OK)
Connection:Close
Content-Length:944
Cache-Control:private, max-age=0
Content-Type:text/xml; charset=utf-8
Date:Sat, 10 Dec 2005 23:59:55 GMT
Server:ASP.NET Development Server/8.0.0.0
X-AspNet-Version:2.0.50727
<?xml version="1.0" encoding="utf-16"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetWeblogDebugResponse xmlns="http://tempuri.org/">
<GetWeblogDebugResult>
<weblogChangeOnly>true</weblogChangeOnly>
<epropStatusChange>0</epropStatusChange>
<UserId>41</UserId>
<UniqueId>992</UniqueId>
<Privacy>2</Privacy>
<Title>Hello</Title>
<Body>¤é¸g«ü¼Æ¬Q¦¬¦Ü15551.31¡A¦]§ë¸ê¤H¶R¶i¿ûÅKªÑ¡A³Ð³æ¤é¦¨¥ ¬ö¿ý·s°ª¡Cserverinsert</Body>
</GetWeblogDebugResult>
</GetWeblogDebugResponse>
</soap:Body>
</soap:Envelope>
----------------------------------
WebServiceStudio 2.0:
http://www.gotdotnet.com/Community/U...4-e916ebc4159c