0

I'm running into an issue with WebRequest when the POST contains an ampersand. When attempting to get the response, I am being thrown a 400 Bad Request error. I've tried HttpServerUtility.UrlEncode on the postData variable, but that throws an error as well.

Here is my code:

Dim request As WebRequest
Dim postData As String = String.Empty
Dim byteArray As Byte()
Dim dataStream As Stream = Nothing
Dim response As WebResponse = Nothing

Dim reader As StreamReader = Nothing
Dim responseFromServer As String
request = WebRequest.Create(_strWSURL)
request.Method = "POST"
postData = "p_input_xml_doc=<?xml version=""1.0"" encoding=""UTF-8""?>
<XMLRootNode>
 <Transactions>
  <Transaction>
   <Data>FirstNode</Data>
  </Transaction>
  <Transaction>
   <Data>Second&amp;Node</Data>
  </Transaction>
 </Transactions
</XMLRootNode>"

byteArray = Encoding.UTF8.GetBytes(postData)
request.ContentType = "text/xml; encoding='utf-8'"
request.ContentLength = byteArray.Length
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
response = request.GetResponse()'<--Thorws "The remote server returned an error: (400) Bad Request."

dataStream = response.GetResponseStream()
reader = New StreamReader(dataStream)
responseFromServer = reader.ReadToEnd()
  • What type of service are you calling? WCF? Did you write it? Did you debug it? Have you tried using fiddler to inspect what's going over the wire? – Rick S Oct 07 '14 at 20:52
  • you could add [<[[CDATA]]>] tags to the raw content of the nodes, however, i agree with @RickS in that sense that the request looks a bit odd http://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean – Icepickle Oct 07 '14 at 21:19
  • It was not written by me but a partner that we are working with. Is it possible that this is an error on their end? I assumed that since it's a bad request error, that it has something to do with the HTTP request. – Justin Whitehead Oct 08 '14 at 01:53

0 Answers0