Home Special character not encoded properly during asmx webrequest
Reply: 1

Special character not encoded properly during asmx webrequest

Aswin Francis
1#
Aswin Francis Published in 2018-02-14 07:02:21Z

Below is the webmethod in asmx that get the request xml

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

The XML request is as below:

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

I am expecting the above XML to be displayed by the asmx with the BillingAddress1 value as seen above but what i get is as seen below:

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

How can i get the those special characters to be displayed properly?

Sunil
2#
Sunil Reply to 2018-02-14 08:41:04Z

If you notice the encoding got changed from UTF-8 to iso-8859-1

You need to set the encoding in the StringWriter, unfortunately it doesn't support setting encoding straight-forwardly. So create your own StringWriter by inheriting from StringWriter.

Public Class MyStringWriter
    Inherits StringWriter

    Public Overrides Property Encoding As Encoding
        Get
            Return Encoding.UTF8
        End Get
    End Property
End Class

Then use in your code:

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)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.423543 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO