在asmx webrequest期间,特殊字符未正确编码

.net xml vb.net web-services

72 观看

1回复

33 作者的声誉

下面是asmx中获取请求xml的web方法

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

如何才能正确显示这些特殊字符?

作者: Aswin Francis 的来源 发布者: 2018 年 2 月 14 日

回应 1


1

3108 作者的声誉

决定

如果您注意到编码已更改UTF-8iso-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)
作者: Sunil 发布者: 2018 年 2 月 14 日
32x32