Getting WebException on every other attempt to UploadData

Hi,

I am trying to POST data to webpage. My code below works fine the first time, and I am able to read the response. But on the second execution of the same code, I get an error that says:
The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

When in debug mode, the exception occurs and it stops on the line:
Dim res As Byte() = web.UploadData(strWebpage, "POST", byteJobDetails)

But, I can tell it to step to next line, and it completes the code and the data is actually posted to the webpage.

After the second attempt complains with the error, I am able to run the same function again and it works fine (fails the fourth....). Shouldn't web.Dispose() take care of closing all connections and =Nothing clean-up the allocated memory

CODE:

Answer this question

Getting WebException on every other attempt to UploadData

  • Plume-Tsee

    Thanks for the suggestions, but it doesn't seem to help:

    I tried adding the following to my code:

    Dim web As System.Net.WebClient = New System.Net.WebClient
    web.Headers.Add(Net.HttpRequestHeader.KeepAlive,
    "False")
    web.Headers.Add(Net.HttpRequestHeader.ContentType,
    "application/x-www-form-urlencoded")
    Dim res As Byte() = web.UploadData(strWebpage, "POST", byteJobDetails)

    and I still got the same Error messages... sometimes the error is "An exception occurred during a WebClient request." (trouble shooting tips suggest looking at more information on "inner exceptions", checking Response or Status) and other times it returns the error about losing the connect.

    I am unable to change the code that is running on the server side, and I do not know if the keep-alive is set on the server or not.  This is my first time working with the webclient class and communicating this way with a server, so any suggestions on where to learn more about this would be appreciated (and keep me from asking simple beginner questions).

    The thing that stumps me is why does it work every other time if there is a problem with the connection   It seems to me that if it fails on the second time, it should also fail on the 3rd, 4th, etc.

    As I said before, this is my first time working with both the webclient and the POST/GET methods, so it is likely that I haven't learned enough on the topic to fully understand what needs to be done in my code to properly send the information.

     

    Thanks again for your help so far and any more you may be able to provide


  • Kurt Vergeyle

    Looks like WebClient has Keep Alive set to true by default .

    I could not find any property on WebClient that you can change to set KeepAlive to false, but there is a  Headers property using which you could try setting header Connection to a value of  close



  • RHocking

    Thanks for the tip Malar.

    I've been playing around with the HttpWebRequest class and it seems to be working now  I'll post the working code below in case anybody else is having a similar problem.  Essentially all the code is the same except I declared my variable 'request' by using "request = Net.WebRequest.Create(strWebpage)".

     

    -----CODE------

    Dim strWebpage As String
    Dim request As System.Net.HttpWebRequest

    strWebpage = getWebpage() & "/SubmitJob"
    request = Net.WebRequest.Create(strWebpage)
    request.Method =
    "POST"
    request.ContentType = "application/x-www-form-urlencoded"
    Dim data As String = getBuildCommand(CmdFormat.HTTP)    'builds the list of variable:   " resp=text&jobname=JobName&..."
    Dim bytes As Byte()

    If data.StartsWith("Error:") <> True Then  'getBuildCommand returns the string "Error:" if it encountered problems...
        bytes = System.Text.Encoding.UTF8.GetBytes(data)
    Else
       
    MsgBox(data.Replace("Error:", "").Trim())
       
    Exit Sub
    End If

    request.ContentLength = bytes.Length
    Try   'For proper error handling if webserver not available
       
    Dim request_stream As System.IO.Stream = request.GetRequestStream()
        request_stream.Write(bytes, 0, bytes.Length)
       
    Dim response As System.Net.HttpWebResponse = request.GetResponse()
       
    If response.StatusCode = System.Net.HttpStatusCode.OK Then
           
    DebugPrint("POST Method returned status OK")
           
    Dim receiveStream As IO.Stream = response.GetResponseStream
           
    Dim readStream As New IO.StreamReader(receiveStream, System.Text.Encoding.UTF8)
            data = readStream.ReadToEnd()
            DebugPrint(
    "Response stream received from Build Server: " & vbCr & data)
            data = data.Replace(
    "submitted", "").Replace("Job", "").Trim
            DebugPrint(
    "Job #" & data & " has been Started on " & getComputerName())
            ts_Status.Text =
    "Job " & txt_JobName.Text & " has been Started on " & getComputerName() & " as job #" & data
            readStream.Close()
            receiveStream.Close()
       
    Else
           
    MsgBox("There was an error Submitting the job to " & getComputerName())
       
    End If

       
    request_stream.Close()
        response.Close()

    Catch ex As Exception
        MsgBox(
    "There was an error communicating with " & getComputerName())
    End Try

    ----END CODE-----

    The other major change was that I used the Streams to get the response from the server.  I can't say I'm a pro yet, but I've got it working.  Thanks everybody for all the tips.


  • pablex

    Hi Lucian,

    I do not have access to the properties on the web site and cannot configure Keep-Alive property.

    I'm not familiar with web service. When searching the internet for code examples on what I wanted to do, I kept running across the webclient class. I have been able to implement it and it works perfect the first time; I am able to see the server taking action on the information, and I also am able to read the response that the server replies with in my code.

    Perhaps if I explain what my program is doing...
    At work we submit jobs to a server by going to a webpage on a server, filling out a form with what we need done and submitting it. In my job, I am scheduling these task, with a minor change to the job very often, but the webpage doesn't remember my previous settings. My program allows me to remember settings/defaults and simply provides an easier interface.

    This is my first experience using the POST/GET methods, and most of my programming experience prior to this is embedded systems and a few Front-End database forms where everything resides on the local machine. So I am a total newbie when it comes to Forms on webpages and POSTing or GETting data.

    If I could just figure out how to work around this problem with the webclient not allowing a second submission then my program will work as needed.

    Thanks for your help and quick responses.


  • kareddy

    You can use a tool such as Fiddler to see what happens on the wire. Keep-Alive can be configured in the IIS admin console, Properties on your web site. I see that you are trying to post something to a web form. Can't you just use a web service to do what you want
  • Kennibal

    I am not sure if webclient allows you to change the existing headers. but here are some other tricks that can help you do that, incase network sniffers like netmon shows that the  keepalive header is not sent to the server

    GetWebRequest is a protected function in webclient. So extend webclient and set the keepalive property in the webrequest obtained from the GetWebRequest function to false.

    WebClient is a simple abstraction over HttpWebRequest.So whatever you are trying to do with webclient should be doable with HttpWebRequest. So instead of using webclient use HttpWebRequest.

     

     


  • richardjjs

    Maybe Keep-Alive is not enabled on the server.
  • Getting WebException on every other attempt to UploadData