特殊字符在ASMX WebRequest期间未正确编码



以下是ASMX中获取请求xml

的WebMethod
 <WebMethod>
Public Function SubmitOrder(SubmitOrderRequest As SubmitOrderRequest) As SubmitOrderResponse  Implements IIHybrisOrderImportServiceSoapBinding.SubmitOrder
Dim inputserilize As New XmlSerializer(SubmitOrderRequest.GetType)
    Dim strwriters = New StringWriter
    inputserilize.Serialize(strwriters, SubmitOrderRequest)
    WriteToFile("Input XML: " & strwriters.ToString & vbCrLf)

XML请求如下:

<?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>
<ns2:SubmitOrderRequest xmlns:ns2="http://cos.ws.sideup.reply.eu/SubmitOrderRequest">
        <Orders>
        <Order>
            <Action>CREATE</Action>
   <BillingAddress>
    <BillingAddress1>Paulñ Pogbaà</BillingAddress1>

我希望上述XML由ASMX显示,带有BillingAddress1值,如上所述,但我得到的如下所示:

<?xml version="1.0" encoding="iso-8859-1"?>
<SubmitOrderRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Orders>
    <Order>
      <Action>CREATE</Action>
      <BillingAddress>
        <BillingAddress1>Paul?? Pogba??</BillingAddress1>

如何获得正确显示的特殊字符?

如果您注意到编码从UTF-8更改为iso-8859-1

您需要在StringWriter中设置编码,不幸的是,它不支持直接编码的设置。因此,通过从StringWriter继承来创建自己的StringWriter

Public Class MyStringWriter
    Inherits StringWriter
    Public Overrides Property Encoding As Encoding
        Get
            Return Encoding.UTF8
        End Get
    End Property
End Class

然后在您的代码中使用:

Dim inputserilize As New XmlSerializer(SubmitOrderRequest.GetType)
Dim strwriters = New MyStringWriter
inputserilize.Serialize(strwriters, SubmitOrderRequest)
WriteToFile("Input XML: " & strwriters.ToString & vbCrLf)
Dim inputserilize As New XmlSerializer(SubmitOrderRequest.GetType)
Dim strwriters = New StringWriter
inputserilize.Serialize(strwriters, SubmitOrderRequest)
WriteToFile("Input XML: " & strwriters.ToString & vbCrLf)

相关内容

  • 没有找到相关文章

最新更新