views:

280

answers:

2

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

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

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.

Thanks.

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.

+1  A: 

I'm not a web service expert, but I compared your code to some working code I have, and the only relevant difference is that you are not setting the ContentLength of your request.

myWebRequest.ContentLength = strDataToPost.Length()
Jason Berkan
Thanks for the help Jason, I've now added the contentlength and am still getting the 400 error. I'm glad to see that my code 'should' be working, so I think its time to look into the service itself.
Phil
+1  A: 

The data you're trying to post might look a little funny if you're concatenating a time string to it:

strDataToPost = DataToSend.Text & Server.UrlEncode(Now())

If DataToSend is proper XML, then you're adding the Url Encoding of Now() which makes me think it will no longer be valid XML.

Check to make sure your StreamWriter is not inserting additional characters (CR, LF). If it does, then the length you're sending does not correspond to the actual data, but that probably wouldn't have caused a problem before you started sending the content length.

Is your web service configuration to accept XML directly? I'm wondering if you might have to encapsulate the XML in multipart/form-data in order for your web service to accept it.

David Smith
David your a legend! I removed the encoding and it now works fine!
Phil