HTTPPost WebException Connection Closed

G

Guest

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
 
J

Jeffrey Tan[MSFT]

Hi,

For this issue, I will do some research on it, and reply to you some time
later. Thanks for your understanding!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi Jeffery,

In the process of putting together a sample app to demonstrate the problem,
I found it. Before calling the routine to do the HTTP Post, I was calling
the following routine to determine if the user was connected to the Internet:

Public Shared Function IsConnectedToInternet(ByVal URL As String, ByVal
TimeoutMilliSeconds As Integer) As Boolean

Dim Request As HttpWebRequest
Dim Response As HttpWebResponse
Dim IsConnected As Boolean = False

Try
Request = CType(WebRequest.Create(URL), HttpWebRequest)
Request.Timeout = TimeoutMilliSeconds
Response = CType(Request.GetResponse(), HttpWebResponse)
Request.Abort()
If Response.StatusCode = HttpStatusCode.OK Then
IsConnected = True
End If
Catch Ex As Exception
IsConnected = False
End Try
IsConnectedToInternet = IsConnected
End Function

The problem appears to be aborting the request before closing the response.
When I moved the abort to the last line of the finally block, it cleared up
the problem.

Thanks for your help.
 
G

Guest

Here's the version of the ConnectedToInternet routine:

Public Shared Function IsConnectedToInternet(ByVal URL As String, ByVal
TimeoutMilliSeconds As Integer) As Boolean
Dim Request As HttpWebRequest
Dim Response As HttpWebResponse
Dim IsConnected As Boolean = False

Try

Request = CType(WebRequest.Create(URL), HttpWebRequest)
Request.Timeout = TimeoutMilliSeconds
Response = CType(Request.GetResponse(), HttpWebResponse)
If Response.StatusCode = HttpStatusCode.OK Then
IsConnected = True
End If
Catch Ex As Exception
IsConnected = False
Finally
If Not Response Is Nothing Then
Response.Close()
End If
Request.Abort()
End Try

IsConnectedToInternet = IsConnected

End Function
 
J

Jeffrey Tan[MSFT]

Hi,

I am glad your problem resolved. Anyway, if you need further help, please
feel free to tell me, I will help you. Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

The problem listed below occurs when trying to add web refernces that are
on-line.
-------------------------------------------------------------------------------------------
There was an error downloading
'http://ws.cdyne.com/delayedstockquote/delayedstockquote.asmx'.

The underlying connection was closed: The proxy name could not be resolved,
verify correct proxy configuration.
------------------------------------------------------------------------------------------
I changed my machine.config file, but the problem still exists.

<defaultProxy>
<proxy
usesystemdefault = "false"
proxyaddress="http://proxyserver:80"
bypassonlocal = "true" />
</defaultProxy>

+++++++++++++++
My system is as follows:
VS.NET 2003 (7.1.3088) and .Net v1.1 sp1 (1.1.4322 SP1).
The server is Windows 2003 running IIS 6.0.3790.0
+++++++++++++++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top