2012-08-13 49 views
0

我正在使用Magento 1.7及其SOAP API來將訂單檢索到我的.Net環境。 除了字符編碼以外,一切都可以。 Chars,如Å,Ä和Ö被翻譯爲??。無法反序列化網絡服務中的斯堪的納維亞語特徵

來自Fiddler的響應顯示服務器(Litespeed)實際上以UTF-8發送更正字符。 MySql數據庫也以UTF-8編碼。

提琴手原料:

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

如何我的C#客戶端強制不使這是任何想法? ?? ?? ..?

+0

我不認爲有很多人知道Magento。如果可以的話 - 將發佈加載這些響應的代碼是很好的。 – outcoldman 2012-08-13 14:57:22

+1

這不是Magento的錯(無論Magento是什麼),但客戶端代碼不能正確解釋響應。 – 2012-08-13 15:00:04

+0

我從來沒有說過它是Magentos的錯,我在問我如何強制我的客戶端正確解釋響應。 – 2012-08-14 10:46:08

回答

0

我去徹底堅果和解決它在可能最壞的方式,但它確實幫助我完成我的任務,因爲設置的CultureInfo或設置requestencoding上的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 
相關問題