This is very frustrating and I believe it to be a bug in the framework. We have tried everything we can think of to fix this and it happens consistently.
This statement in the below code:
Dim WebResp as HTTPWebResponse = DirectCast WebReq.GetResponse(), HttpWebResponse)
That statement will hang every single time if the user closes or clicks back on the browser and tries the button click again. It will work the first time, but hangs until timeout in subsequent attempts. Every time. Closing the streams, etc. doesn't fix it. This happens on multiple machines and platforms as well as Framework 1.1 and 2.
'Build Web Request
Dim WebReq As HttpWebRequest = DirectCast(WebRequest.Create(strPage), HttpWebRequest)
'Authentication
Dim auth As String = "xxxxxxxx

xxxxxxxf"Dim authBytes As Byte() = Encoding.UTF8.GetBytes(auth.ToCharArray())
WebReq.Headers("Authorization") = "Basic " & Convert.ToBase64String(authBytes)
'Our method is post, otherwise the buffer (postvars) would be useless
WebReq.Method = "POST"
'We use form contentType, for the postvars.
WebReq.ContentType = "application/x-www-form-urlencoded"
'The length of the buffer (postvars) is used as contentlength.
WebReq.ContentLength = buffer.Length
'We open a stream for writing the postvars
Dim PostData As Stream = WebReq.GetRequestStream() *** hangs here ***
'Now we write, and afterwards, we close. Closing is always important!
PostData.Write(buffer, 0, buffer.Length)
' Close the Stream
PostData.Close()
'Get the response handle, we have no true response yet!
Dim WebResp As HttpWebResponse = DirectCast(WebReq.GetResponse(), HttpWebResponse)
Dim strResponse As String
strResponse = WebResp.StatusCode
Dim streamResponse As Stream = WebResp.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim responseString As String
responseString = streamRead.ReadLine
streamResponse.Close()
streamRead.Close()
WebResp.Close()
streamResponse = Nothing
streamRead = Nothing
WebResp = Nothing
GC.Collect()
' The Following code gets the return variables into pairs
Dim rPairs As New NameValueCollection
Dim pairs As String() = responseString.Split("&"c)
For Each pair As String In pairsDim values As String() = pair.Split("="c)
rPairs.Add(values(0), values(1))
Next
If strResponse = HttpStatusCode.OK Then
If rPairs("response_code") = "1" Then
' Collect Garbage and see if that will end the session
GC.Collect()
HttpContext.Current.Response.Redirect(rPairs("cons umer_url"))
rPairs = Nothing
HttpContext.Current.Response.Close()
HttpContext.Current.Response.End()
Return True
Else
'Tools.EventLogger.LogEvent((rPairs("response_code ") & ":") + rPairs("response_msg"), EventLogEntryType.[Error])
HttpContext.Current.Response.Write("Status1 Code: " & WebResp.StatusCode)
Return False
End If