title: Post XML to a web service
body: <p>I have a web service, which accepts XML input. What I am trying to do is setup an aspx page which posts xml to the service. Here is my code so far, but I am getting an error 400 (bad request) when I try to submit...</p>
  
  <pre><code>Imports System.Net
  Imports System.IO
  
  Partial Class _Default
      Inherits System.Web.UI.Page
  
      Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
          Dim strDataToPost As String
          Dim myWebRequest As WebRequest
          Dim myRequestStream As Stream
          Dim myStreamWriter As StreamWriter
          Dim myWebResponse As WebResponse
          Dim myResponseStream As Stream
          Dim myStreamReader As StreamReader
  
  
          ' Create a new WebRequest which targets the web service method
          myWebRequest = WebRequest.Create("http://foo/p09SoapHttpPort")
  
          ' Data to send 
          strDataToPost = DataToSend.Text &amp; Server.UrlEncode(Now())
  
          ' Set the method and content type
          With myWebRequest
              .Method = "POST"
              .ContentType = "text/xml"
              .Timeout = -1
              .ContentLength = strDataToPost.Length()
  
          End With
  
  
  
          ' write our data to the Stream using the StreamWriter.
          myRequestStream = myWebRequest.GetRequestStream()
          myStreamWriter = New StreamWriter(myRequestStream)
          myStreamWriter.Write(strDataToPost)
          myStreamWriter.Flush()
          myStreamWriter.Close()
          myRequestStream.Close()
  
          ' Get the response from the remote server.
          myWebResponse = myWebRequest.GetResponse()
  
          ' Get the server's response status
          myResponseStream = myWebResponse.GetResponseStream()
          myStreamReader = New StreamReader(myResponseStream)
          ResponseLabel.Text = myStreamReader.ReadToEnd()
          myStreamReader.Close()
          myResponseStream.Close()
  
          ' Close the WebResponse
          myWebResponse.Close()
  
      End Sub
  End Class
  </code></pre>
  
  <p>If anyone knows of any good web resources on how to upload .xml files to a web service method that would also be a great help and would answer this question as I can re-work it that way.</p>
  
  <p>Thanks.</p>
  
  <p>P.S in the last edit, I modified the code to have .contentlength (thanks for the assistance). Unfortunately after this I am still getting 'Bad Request'. If anyone can confirm / disconfirm my code should be working, I will start investigating the service itself.</p>