Hello,

I need help with something it drives me nuts. I have an application which send XML to a web server through a POST method. This part works excellent. But where I have issues is with receiving the standard response from the web server for OK or error. Instead of receiving the actual response, I get the error message" WebException was unhandled. The server committed a protocol violation. Section=ResponseStatusLine" . The response I should receive is in XML format where I have as elements the error result (ok or error), the error number and the error description. Here is the code I have:

Public Sub REQRESP()

        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader

        ' Create a request using a URL that can receive a post. 
        Dim request As HttpWebRequest = HttpWebRequest.Create("http://"192.168.0.1/test/test.xml")
        ' Set the Method property of the request to POST.
        request.Method = "POST"


        ' Create POST data and convert it to a byte array.
        Dim postData As String = _
        "<TEST INFO='TEST'><CITY CODE='PO'>test</CITY></TEST>"

        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

        ' Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded"
        ' Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length
        ' Get the request stream.
        Dim dataStream As Stream = request.GetRequestStream()
        ' Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length)
        ' Close the Stream object.
        dataStream.Close()

        Try
            ' Get response   
            response = DirectCast(request.GetResponse(), HttpWebResponse)

            ' Get the response stream into a reader   
            reader = New StreamReader(response.GetResponseStream())

            ' Console application output   
            Label1.Text = reader.ReadToEnd()
        Finally
            If Not response Is Nothing Then response.Close()
        End Try

    End Sub

The code stops at "response = DirectCast(request.GetResponse(), HttpWebResponse)" and gives me the above mentioned error. I searched the web for an answer to this but no success so far. Hopefully here is the right place to ask for help with this issue I have.

Thank you!

>I searched the web for an answer to this but no success so far.

Me too. I think you want to request am xml document and return something. Isn't it? Please be specific about your question.

Hi adatapost,

What I want is to know how to get the response from the web server. The response is sent by the web server in XML format:
<TEST RESULT="ERROR">
<ERRORNUMBER>100</ERRORNUMBER>
<ERRORDESCRIPTION>Parse error</ERRORDESCRIPTION>
</TEST>

Aldough the response part of the code looks correct, I don't get it. Instead I received the previously mentioned error. Please let me know if further clarifications are required.

Thanks!

>What I want is to know how to get the response from the web server. The response is sent by the web server in XML format

It means that following is the content of test.xml file. Isn't It?

<TEST RESULT="ERROR">
    <ERRORNUMBER>100</ERRORNUMBER>
    <ERRORDESCRIPTION>Parse error</ERRORDESCRIPTION>
</TEST>

No, in the test.xml I save the "postData" string I am sending through HttpWebRequest and the POST method. Then, depending if this action was succesful or not, the web server will give me a message in the format I mentioned earlier.

Thank you for your reply but that's not it. I didn't had this in my code. I did this typo here when I give an example of the code I use.

I found the answer after so much time of searching it. To get the response on VB.NET you need to add this to the app.config file (right before the </configuration>) :
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true" />
</settings>
</system.net>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.