Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Latin1 (ISO-88591-1) encoding SOAP Request

500 views
Skip to first unread message

Eric Nichols

unread,
Oct 17, 2002, 2:01:57 PM10/17/02
to
I have been unsuccessful at getting a .NET Web Service to respect the
encoding/charset of XML nodes sent as parameters of web service function
calls. Has anyone been able to get this to work using an encoding besides
UTF-8? Here are some details:

I am using VS.NET to create a web service in C#. I have set the
globalization parameters in the Web.config file to use ISO-8859-1 as both
the requestEncoding and the responseEncoding. (Note that regardless of these
settings, the XML returned by the web service uses UTF-8... that seems to be
a bad sign, although I'm not as worried about the response as I am parsing
the request.)

I am using VB 6 to generate SOAP calls to the web service. I generate my
own proxy classes in VB and use my own SoapClient and SoapReader classes
that I wrote (these provide the same functionality of the SOAP Toolkit, with
the addition of an asynchronous SOAP call model to make up for the SOAP
toolkit deficiency of only providing synchronous calls to the SOAP methods,
as well as providing the ability to specifiy the desired encoding). I use
the XMLHTTP30 class to make the SOAP calls. I specify the content-type of
text/xml as well as charset=ISO-8859-1. I make sure to include the XML
declaration at the start of the XML content that specifies the encoding,
<?xml version=1.0" encoding="iso-8859-1" ?>

All of this works great and I have web services in production using all this
code. The only problem is that the XML encoding is not respected at some
point: the parameter to my web service method is a .NET XmlNode, but when I
look at the contents of the XmlNode any ISO-8859-1 but non-ASCII characters
have been turned into question marks, "?".

Does anyone have any suggestions on what I may be doing wrong? Are the
globalization elements of Web.config known to be functional?

Thanks in advance,
Eric Nichols
Software Engineer

eurico

unread,
Oct 17, 2002, 6:53:12 PM10/17/02
to
Hi Eric,

Have you found any solution for this problem? I'm having the exact
same problem and I cannot figure out how to change the encoding of my
web service from utf-8 to iso-8859-1. when I send a soap message to my
web service with latin characters, they are removed from my message,
and that is causing allot of problems.

If you have found a way to change this, let me know.
Thanks allot,
Eurico

"Eric Nichols" <eric.n...@intellichem.com> wrote in message news:<#wU7AdgdCHA.3556@tkmsftngp08>...

Eric Nichols

unread,
Oct 17, 2002, 8:05:06 PM10/17/02
to
Hi Eurico,
It's sort of comforting to know I'm not the only one with this problem. I
have spent as much time as is reasonable trying to fix this, and I've still
had no luck despite much effort. Does any one else have any advice or a
similar problem?

If this is a .NET problem as opposed to user error, I would like to know
so that I could plan accordingly. Currently I only have a short list of
non-ASCII characters that I need to support, so I do a clunky
search-and-replace on known strings that will contain question marks "?"
when the encoding isn't working, but this is a bad solution and won't work
in the long term. Also, I'm stuck with ISO-8859-1 as it is used throughout
our architecture; switching to UTF-8 is not an option.

Hoping for some advice,
Eric Nichols


"eurico" <ejc...@oninet.pt> wrote in message
news:67235c8e.02101...@posting.google.com...

Yann Christensen

unread,
Oct 18, 2002, 4:45:29 PM10/18/02
to
Hi,

I'm trying to get a clear answer on this. Hopefully I will have something
today.

thanks,
Yann


"Eric Nichols" <eric.n...@intellichem.com> wrote in message

news:OnFI8njdCHA.2392@tkmsftngp08...

Pierre Greborio

unread,
Oct 18, 2002, 4:56:28 PM10/18/02
to
I don't know if this can help, but here you can change the encoding to soap
message. I implemented a SoapExtension. That is just a starting point...

using System;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.IO;

using System.Diagnostics;

using System.Xml;

using System.Text;

namespace WSStudy

{

public class EncodingExtension : SoapExtension

{

Stream newStream;

Stream oldStream;

Encoding encoding;


public override object GetInitializer(LogicalMethodInfo methodInfo,
SoapExtensionAttribute attribute)

{

return ((EncodingExtensionAttribute) attribute).Encoding;

}

public override object GetInitializer(Type WebServiceType)

{

return WebServiceType.GetType();

}

public override void Initialize(object initializer)

{

encoding = (Encoding)initializer;

}

public override void ProcessMessage(SoapMessage message)

{

switch (message.Stage)

{

case SoapMessageStage.BeforeDeserialize:

WriteInput( message );

break;


case SoapMessageStage.AfterDeserialize:

break;


case SoapMessageStage.BeforeSerialize:

break;


case SoapMessageStage.AfterSerialize:

WriteOutput( message );

break;


default:

throw new Exception("invalid stage");

}

}

public override Stream ChainStream( Stream stream )

{

oldStream = stream;

newStream = new MemoryStream();

return newStream;

}

public void WriteOutput( SoapMessage message )

{

newStream.Position = 0;

TextReader reader = new StreamReader(newStream);

TextWriter writer = new StreamWriter(oldStream);

StringBuilder sb = new StringBuilder(reader.ReadToEnd());

sb.Replace("utf-8", encoding.WebName);

writer.WriteLine(sb);

writer.Flush();

}

private void WriteInput(SoapMessage message)

{

Copy(oldStream, newStream);

newStream.Position = 0;

}

private void Copy(Stream sourceStream, Stream destinationStream)

{

TextReader reader = new StreamReader(sourceStream);

TextWriter writer = new StreamWriter(destinationStream);

writer.WriteLine(reader.ReadToEnd());

writer.Flush();

}


}

[AttributeUsage(AttributeTargets.Method)]

public class EncodingExtensionAttribute : SoapExtensionAttribute

{

private Encoding encoding = Encoding.GetEncoding("ISO-8859-1");

private int priority;

public override Type ExtensionType

{

get { return typeof(EncodingExtension); }

}

public override int Priority

{

get { return priority; }

set { priority = value; }

}

public Encoding Encoding

{

get { return encoding; }

set { encoding = value; }

}

}

}

Pierre
--
---------------------------------------------------------------
Pierre Greborio
pie...@pierregreborio.it
http://www.pierregreborio.it
UGIDOTNET http://www.ugidotnet.org
---------------------------------------------------------------


Yann Christensen

unread,
Oct 18, 2002, 5:23:32 PM10/18/02
to
.NET asmx web services will determine the character encoding of the request
by looking at the charset directive on the Content-type http header. If it
is not specified then we must assume us-ascii as-per the rules of the
text/xml mime-type. We do NOT look at Content-encoding or at the httpRuntime
config settings for requestEncoding/responseEncoding.

So your Content-type header should look something like this:
Content-type: "text/xml; charset='iso-8859-1'"

Also, have you done a netmon sniff to verify that the data on the wire is
properly encoded?

thanks,
Yann


"Yann Christensen" <xws...@online.microsoft.com> wrote in message
news:Od8ddcudCHA.1476@tkmsftngp10...

Pierre Greborio

unread,
Oct 18, 2002, 5:58:17 PM10/18/02
to
Yann,
I just made a simple test setting the encoding (ser.RequestEncoding =
System.Text.Encoding.GetEncoding("ISO-8859-1");) before to call the web
method and effectively the SOAP call is correctly encoded:

<?xml version="1.0" encoding="iso-8859-1" ?>
- <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>
<GetPerson xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>

The problem is that the response stream contains always the same encoding:

<?xml version="1.0" encoding="utf-8" ?>
- <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>
- <GetPersonResponse xmlns="http://tempuri.org/">
- <Persona>
<FirstName>Pierre</FirstName>
<LastName>Greborio</LastName>
- <Fc>
<FC>FFGFDRE54654DFD</FC>
</Fc>
</Persona>
</GetPersonResponse>
</soap:Body>
</soap:Envelope>

The only solution I found was to implement a SoapExtension as you can see on
this thread.

Thanks
Pierre

--
---------------------------------------------------------------
Pierre Greborio
pie...@pierregreborio.it
http://www.pierregreborio.it
UGIDOTNET http://www.ugidotnet.org
---------------------------------------------------------------

"Yann Christensen" <xws...@online.microsoft.com> wrote in message

news:evewuxudCHA.1544@tkmsftngp09...

Eric Nichols

unread,
Oct 18, 2002, 7:48:24 PM10/18/02
to
Yann,
Thanks for the reply. I do believe that my Content-type header is correct
and that the data on the wire is properly encoded, but I not not sure about
2 things:

1. I am using the SOAP toolkit's (v3) Trace Utility to see the SOAP
request, as opposed to using netmon; on my development workstation I don't
believe I have netmon installed and I've never used it. Should I do so?

2. When I look at the message in the Trace Utility it starts out like this:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
- <SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
- <SOAP-ENV:Body>

but when I look at the HTTPHeaders in the Trace Utility, I see:
...
<content-type>text/xml</content-type>
...

Perhaps this is the problem, because the charset part of the content-type
isn't displayed here. But I do set it when I create the HTTP request.
Here's the VB client code I'm using. g_xhr is an instance of XMLHTTP30.

' Connect to the web service
g_xhr.open "POST", g_strEndPointURL, bolAsync
g_xhr.setRequestHeader "Content-Type", "text/xml; charset='ISO-8859-1'"

Do you see anything wrong with this? I don't know if this is really setting
the charset properly or not. However, when I view the content in the SOAP
message with the Trace Utility, my Latin characters do show up fine. Of
course, I still have the problem where in the Web Service code the latin
characters have become ? marks.


Please let me know if you think I'm doing something wrong here... and
thanks again for the reply.

Best Regards,
Eric Nichols
Software Engineer


"Yann Christensen" <xws...@online.microsoft.com> wrote in message

news:evewuxudCHA.1544@tkmsftngp09...

Eric Nichols

unread,
Oct 18, 2002, 7:50:54 PM10/18/02
to
Pierre,
Thanks for the help! I was definitely hoping for a simpler fix, but if
this does the trick I'm happy about that. I've never used the SoapExtension
class before so it looks like I need to read up on it.

Cheers,
Eric

"Pierre Greborio" <pie...@pierregreborio.it> wrote in message
news:uJiTsiudCHA.1300@tkmsftngp08...

Eric Nichols

unread,
Oct 18, 2002, 7:55:49 PM10/18/02
to
Yann,
Just to clarify, I'm curious about what the
requestEncoding/responseEncoding attributes are used for in Web.config. Am
I correct in thinking that these currently do not have any function? If
they are functional, what do they do? To make sure we're talking about the
same thing, I am looking at the file Web.config for a Web Service project in
C#, where in <system.web> there is a <globalization> node with attributes
called "requestEncoding" and "responseEncoding".

Thanks again,
Eric

"Yann Christensen" <xws...@online.microsoft.com> wrote in message

news:evewuxudCHA.1544@tkmsftngp09...

0 new messages