2

我使用 wsdl.exe 生成代理类来请求 Web 服务,这些服务可能是在 Java 平台上构建的。问题在于响应的编码。我得到'?而不是俄语字母。(例如 '????26' 而不是 'АН26'我也使用soapUI,一切正常。我在配置 .Net 客户端方面没有经验。那么我如何确定和配置正确的响应编码。我已经玩过 app.config 如下:

我在这里附上标题信息。我不知道响应标头中的编码信息...请求标头:

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)

响应标头:

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

解决方案:

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; }
    }
}

而不仅仅是添加

[TraceExtension()] 
attribute for proxy invoke method
4

1 回答 1

4

您可以覆盖GetWebResponse您的代理并更改编码

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;

    }
}
于 2012-09-06T06:44:09.577 回答