无法从Web服务反序列化斯堪的纳维亚字符

时间:2012-08-13 14:54:17

标签: c# web-services magento soap

我正在使用Magento 1.7和他们的SOAP api向我的.Net environement检索订单。 除了characther编码之外,一切都很好。诸如Å,Ä和Ö之类的字符被翻译成??。

Fiddler的回应显示服务器(Litespeed)实际上以UTF-8发送更正字符。 MySql数据库也以UTF-8编码。

小提琴手:

<city xsi:type="xsd:string">Å Ä Ö</city>

有关如何强制我的c#客户端不将其呈现为??的任何想法? ?? ?? ..?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我完全疯了并以最坏的方式解决了它,但它确实帮助我完成了我的任务,因为设置cultureinfo或在webclient上设置请求编码没有帮助:自定义SoapExtension:

Public Class MySoapExtension
Inherits SoapExtension
Private oldStream As Stream
Private newStream As Stream

Public Overrides Function ChainStream(stream As Stream) As Stream
    oldStream = stream
    newStream = New MemoryStream()
    Return newStream
End Function

Public Overloads Overrides Function GetInitializer(methodInfo As  _
    LogicalMethodInfo, _
  attribute As SoapExtensionAttribute) As Object

End Function

Public Overloads Overrides Function GetInitializer(WebServiceType As  _
  Type) As Object

End Function

Public Overrides Sub Initialize(initializer As Object)

End Sub
Public Overrides Sub ProcessMessage(message As SoapMessage)
    Select Case message.Stage
        Case SoapMessageStage.BeforeSerialize
        Case SoapMessageStage.AfterSerialize
            WriteOutput(message)
        Case SoapMessageStage.BeforeDeserialize
            WriteInput(message)
        Case SoapMessageStage.AfterDeserialize
        Case Else
            Throw New Exception("invalid stage")
    End Select
End Sub

' Write the SOAP message out to a file.
Public Sub WriteOutput(message As SoapMessage)
    newStream.Position = 0
    Copy(newStream, oldStream)
End Sub

' Write the SOAP message out to a file.
Public Sub WriteInput(message As SoapMessage)
    Copy(oldStream, newStream)
    newStream.Position = 0
End Sub

Sub Copy(fromStream As Stream, toStream As Stream)
    Dim reader As New StreamReader(fromStream)
    Dim writer As New StreamWriter(toStream)
    writer.WriteLine(ReplaceInvalidChars(reader.ReadToEnd()))
    writer.Flush()
End Sub

Private Function ReplaceInvalidChars(ByVal content As String) As String
    If content.Contains("salesOrderInfo") Then
        Return MagentoHackHelper.Encode(content)
    Else
        Return content
    End If
End Function

End Class

Public Class MagentoHackHelper

Public Shared Function Encode(content As String) As String
    content = content.Replace("Å", "##Aaring##")
    content = content.Replace("Ä", "##Auml##")
    content = content.Replace("Ö", "##Ouml##")

    content = content.Replace("å", "##aaring##")
    content = content.Replace("ä", "##auml##")
    content = content.Replace("ö", "##ouml##")

    Return content
End Function

Public Shared Function Decode(content As String) As String
    content = content.Replace("##Aaring##", "Å")
    content = content.Replace("##Auml##", "Ä")
    content = content.Replace("##Ouml##", "Ö")

    content = content.Replace("##aaring##", "Å")
    content = content.Replace("##auml##", "Ä")
    content = content.Replace("##ouml##", "Ö")

    Return content
End Function

End Class