Resolved Strange Characters in POST WebRequest Response

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
I'm posting a JSON web request and the everything seems to go through properly, but part of the response contains strange characters. I've tried a few different things, like changing the ContentType, but it hasn't fixed the issue.

For what it's worth, the host I'm connecting to states "The character set of all parameters is GB2312" and the return is by XML (Content-type:text/xml) in their documentation. I'm not sure this has any relevance towards my issue, though.

Here is my code
VB.NET:
Dim postData As String = String.Empty
Dim request As WebRequest = WebRequest.Create("http://api.domainremoved.com/")
request.Method = "POST"
postData = "my=post&data=here"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length

Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()

'HANDLE RESPONSE
Dim response As WebResponse = request.GetResponse()
Using dataStream1 As Stream = response.GetResponseStream()
    Dim reader As New StreamReader(dataStream1)
    responseFromServer = reader.ReadToEnd()
    reader.Close()
End Using
response.Close()

MsgBox(responseFromServer)

and here is what the response looks like
VB.NET:
<?xml version="1.0" encoding="gb2312" ?>
<property xmlns="urn:sap:nicenic:params:xml:ns:sap-2.2.0">
<returncode>300</returncode><failreason>0000:�����֤ʧ��</failreason>
</property>

As you can see, everything looks good except one portion, the small portion after 0000:

Any idea what might be causing this or how I can fix it? I would really like to see the proper response being sent. Thanks in advance!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,557
Location
Norway
Programming Experience
10+
StreamReader used like that reads as UTF-8 encoding. Use StreamReader(Stream, Encoding) constructor and specify Encoding.GetEncoding("gb2312")
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,557
Location
Norway
Programming Experience
10+
Also, if you use for example XDocument.Load directly on the response stream it should detect the right encoding from the xml declaration.
 

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
151
Programming Experience
Beginner
Thanks for your reply John! Do I only need to specify the enocding in the streamreader, or in the bytearray as well?

Should I change both of these:
VB.NET:
Dim byteArray As Byte() = Encoding.GetEncoding("gb2312").GetBytes(postData)
Dim reader As New StreamReader(dataStream1, Encoding.GetEncoding("gb2312"))

Or only the Streamreader:
VB.NET:
Dim reader As New StreamReader(dataStream1, Encoding.GetEncoding("gb2312"))
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,557
Location
Norway
Programming Experience
10+
IDK, probably for POST data too.
 
Top Bottom