jk1
jk1

Reputation: 623

soap response encoding '?' characters in all strings instead of russian .Net proxy, Java server(?)

I generate proxy classes with wsdl.exe to request web-services, that are probably build at java platform. The problem is with encoding of response. I get '?' instead of russian letters.(for example '????26' instead of 'АН26') I also use soapUI and everything works well there. I am not experienced at configuring .Net clients. So how I could determine and configure proper encoding for response. I already played with app.config as next:

I attach headers information here. I don't wee encoding info at responce headers... request headers:

Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "urn:#DCSSci_ListFlight_5"
Content-Length: 641
Host: 109.73.1.66:23022
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

response headers:

HTTP/1.1 200 OK
Date: Thu, 06 Sep 2012 03:47:52 GMT
Server: Apache/2.2.10 (Linux/SUSE)
200 OKX-FidelXML-Version: 2.0
Content-length: 15464
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/xml

Solution:

public class TraceExtension : SoapExtension
{
    Stream oldStream;
    Stream newStream;
    public override Stream ChainStream(Stream stream)
    {
        oldStream = stream;
        newStream = new MemoryStream();
        return newStream;
    }
    public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
    {
        return null;
    }
    public override object GetInitializer(Type WebServiceType)
    {
        return null;
    }
    public override void Initialize(object initializer)
    {
    }
    public override void ProcessMessage(SoapMessage message)
    {
        switch (message.Stage)
        {
            case SoapMessageStage.BeforeSerialize:
                break;
            case SoapMessageStage.AfterSerialize:
                newStream.Position = 0;
                Copy(newStream, oldStream);
                break;
            case SoapMessageStage.BeforeDeserialize:
                message.ContentType = "application/soap+xml; utf-8";
                Copy(oldStream, newStream);
                newStream.Position = 0;
                break;
            case SoapMessageStage.AfterDeserialize:
                break;
        }
    }
    void Copy(Stream from, Stream to)
    {
        TextReader reader = new StreamReader(from, System.Text.Encoding.GetEncoding("utf-8"));
        TextWriter writer = new StreamWriter(to, System.Text.Encoding.GetEncoding("utf-8"));
        writer.WriteLine(reader.ReadToEnd());
        writer.Flush();
    }
}

[AttributeUsage(AttributeTargets.Method)]
public class TraceExtensionAttribute : SoapExtensionAttribute
{
    private int priority;
    public override Type ExtensionType
    {
        get { return typeof(TraceExtension); }
    }
    public override int Priority
    {
        get { return priority; }
        set { priority = value; }
    }
}

And than just add

[TraceExtension()] 
attribute for proxy invoke method

Upvotes: 2

Views: 8360

Answers (1)

L.B
L.B

Reputation: 116178

You can override GetWebResponse of your proxy and change the encoding

public class YourProxyClass : SoapHttpClientProtocol
{
    protected override WebResponse GetWebResponse(WebRequest request)
    {
        var response = base.GetWebResponse(request);
        response.Headers["Content-Type"] = "text/xml; charset=utf-8"; //<==
        return response;

    }
}

Upvotes: 5

Related Questions