HTTPPost WebException Connection Closed

From: WaydeWyatt (wwyatt_at_newsgroup.nospam)
Date: 11/01/04


Date: Mon, 1 Nov 2004 14:29:03 -0800

We have a VB.net application that performs an HTTP form post to an ASP.NET
web page. It always works the first time and then generates the following
error every time after that:

System.Net.WebException: The underlying connection was closed: The request
was canceled.
   at System.Net.HttpWebRequest.CheckFinalStatus()
   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
   at System.Net.HttpWebRequest.GetRequestStream()
   at VTLicense.HTTPPost.PostPage(String url, String FieldsAndValues) in
C:\Dev\VB.NET\VisualTour\VTLicense\Reuse\HTTPPost.vb:line 84"

On the client I'm running VS 2003 with latest service packs (7.1.3088) and
.Net v1.1 sp1 (1.1.4322 SP1) under WinXP. The server is Windows 2000 running
IIS5 with latest service packs and .Net Framework 1.1 sp1.

MSKB Article #819450 suggests turning off HTTPKeepAlive on the server.
While this fixes the problem in our test environment, I cannot do this in
production. The article also suggests setting KeepAlive=false on the client
in the webrequest. However, when I try this it has no effect.

One workaround I have found that does work is to place a Thread.Sleep(100)
right after creating the WebRequest. This is a poor solution and I would not
want to put it in production. It seems there is definetly some problem with
the framework.

Does anyone have some better insight into this issue? Is there anything I
can do differently (client side) to fix this problem?

Thanks,

Wayde Wyatt
Here's the code:

 Public Function PostPage(ByVal url As String, ByVal FieldsAndValues As
String) As String

     Dim Response As String = String.Empty

         Dim result As WebResponse

         Try
             Dim req As HttpWebRequest
             Dim RequestStream As Stream
             Dim ReceiveStream As Stream
             Dim encode As Encoding
             Dim sr As StreamReader

             req = CType(System.Net.WebRequest.Create(New Uri(url)),
System.Net.HttpWebRequest)
             req.KeepAlive = False
             req.Method = "POST"
             req.ContentType = "application/x-www-form-urlencoded"
             Dim SomeBytes() As Byte
             Dim UrlEncoded As New StringBuilder
             Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}

             If FieldsAndValues <> Nothing And Not _Cancelled Then
                 Dim i As Integer = 0
                 Dim j As Integer
                 While i < FieldsAndValues.Length
                     j = FieldsAndValues.IndexOfAny(reserved, i)
                     If j = -1 Then
                         
UrlEncoded.Append(HttpUtility.UrlEncode(FieldsAndValues.Substring(i,
FieldsAndValues.Length - i)))
                         Exit While
                     End If
                     
UrlEncoded.Append(HttpUtility.UrlEncode(FieldsAndValues.Substring(i, j - i)))
                     UrlEncoded.Append(FieldsAndValues.Substring(j, 1))
                     i = j + 1
                 End While
                 SomeBytes =
System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToString())
                 req.ContentLength = SomeBytes.Length
                 RequestStream = req.GetRequestStream()
                 RequestStream.Write(SomeBytes, 0, SomeBytes.Length)
                 RequestStream.Close()
             Else
                 req.ContentLength = 0
             End If

             result = req.GetResponse()
             ReceiveStream = result.GetResponseStream()
             encode = System.Text.Encoding.GetEncoding("utf-8")
             sr = New StreamReader(ReceiveStream, encode)

             Dim read(256) As Char
             Dim count As Integer = sr.Read(read, 0, 256)

             Do While count > 0
                 Dim str As String = New String(read, 0, count)
                 Response = String.Concat(Response, str)
                 count = sr.Read(read, 0, 256)
             Loop

             sr.Close()

         Catch Exc As Exception
             _LastError = String.Concat("PostPage ERROR: ", Exc.ToString)

         Finally
             If Not result Is Nothing Then
                 result.Close()
             End If
         End Try
     End If

     PostPage = Response

 End Function



Relevant Pages

  • Replacing characters in WEB text
    ... Dear friend as a routine to replace special character when one has to ... output text on a web page (client side) I have used this one as a quick ... Public Function FixStringForWEB(ByVal Text As String) As String ...
    (microsoft.public.dotnet.framework)
  • Re: Serialize a Dataset
    ... Public Function VenueData(ByVal VenueID As Integer) As DataSet ... with a .NET client. ... Was trying to return as the string of a function call. ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: string formatting...
    ... because this is the first time I've ever in ... Public Function ConcatFix(String1 As String, ... ConcatFix = Buf ...
    (microsoft.public.access.formscoding)
  • Re: Returning different types of objects in a column in a dataset
    ... webservice that returns an object based on the parameter, and my client ... Here is some test code for that: ... Public Function GetDataSet(ByVal ObjectType As String) As DataSet ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Singleton Objects
    ... But I continue to have the same problem,i.e., message from one client ... >> Private Shared message As String ... >> End Sub ... >> Public Function receiveAs String ...
    (microsoft.public.dotnet.framework.remoting)