vb.net WebRequest在包含&符号时发布400 Bad Request

时间:2014-10-07 20:32:53

标签: xml vb.net post webrequest http-status-code-400

当POST包含&符号时,我遇到了WebRequest的问题。当试图获得响应时,我被抛出400 Bad Request错误。我已在HttpServerUtility.UrlEncode变量上尝试了postData,但也会引发错误。

这是我的代码:

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

0 个答案:

没有答案