connection closed on webrequest
Thank you very much for your post here.
The option with keepAlive=false works excellent for me. I was experiencing the following behavior.
I was writing a windows service that would connect to a website, post some data to get a response. Everything worked fine when I ran the component as a console Application in Debug mode on my machine . Once installed as a service every second request failed.
I had to fight with an unvalid certificate for ssl, username, password and a proxyserver which gave me plenty of options to look for.
I finally tried the keepAlive option and everything works fine. I still do not understand the influence of this parameter on the failure of the connection every second time and would be interested in any ideas.
The environment I am working in is Windows 2000 with .NET 1.0.
I created an abstract of my solution as a coding example. Hope this will safe some time for somebody else.
Karl
Imports System.IO
Imports System.Text
Imports System.Threading
Imports System.Net
Imports System.Text.Encoding
Imports System.Security.Cryptography.X509Certificates
Public Class MyCertificateValidation
Implements System.Net.ICertificatePolicy
'This class handles problems with certificates if ssl (https) is used
Public Function CheckValidationResult(ByVal srvPoint As ServicePoint, _
ByVal cert As X509Certificate, ByVal request As WebRequest, ByVal problem As Integer) _
As Boolean Implements ICertificatePolicy.CheckValidationResult
Return True ' Accept all certificates
End Function
End Class
Public Class RequestState
Public request As WebRequest = Nothing
Public requestDocument As String
End Class
Public Class HTMLRequest
'Handler to synchronise with main thread again
Public allDone As New ManualResetEvent(False)
Private _requestDocument As String = "Text to send"
Private _responseDocument As String
Private _web As Net.HttpWebRequest
Private Sub ReadCallback(ByVal asynchronousResult As IAsyncResult)
'Get the information to pass on from the Async Object
Dim myRequestState As RequestState = CType(asynchronousResult.AsyncState, RequestState)
Dim myWebRequest2 As WebRequest = myRequestState.request
Dim requestDocument As String = myRequestState.requestDocument
'Get the stream to write the request to
Dim streamResponse As Stream = myWebRequest2.EndGetRequestStream(asynchronousResult)
Dim bytArray() As Byte = System.Text.Encoding.ASCII.GetBytes(requestDocument)
streamResponse.Write(bytArray, 0, requestDocument.Length)
streamResponse.Close()
'Tell the main thread that writing to stream is complete
allDone.Set()
End Sub
Public Sub retrieve()
Dim bytOutArray() As Byte
Dim objWebResponse As Net.WebResponse = _web.GetResponse
Dim objStream As IO.StreamReader
objStream = New IO.StreamReader(objWebResponse.GetResponseStream(), System.Text.ASCIIEncoding.ASCII)
_responseDocument = objStream.ReadToEnd()
'close all streams
objWebResponse.Close()
objStream.Close()
End Sub
Public Sub send()
'Prepare Object to pass into asynchron call
Dim myRequestState As New RequestState
myRequestState.request = _web
myRequestState.requestDocument = _requestDocument
Dim r As IAsyncResult = CType(_web.BeginGetRequestStream(AddressOf ReadCallback, myRequestState), IAsyncResult)
'Wait till asychrone is done ## allDone.Set() ##
allDone.WaitOne()
End Sub
Public Sub getRequest()
'Configure connection
'in case of problem with certificate
ServicePointManager.CertificatePolicy = New MyCertificateValidation
'Create request object
_web = CType(WebRequest.Create("https://www.somedomain.com"), HttpWebRequest)
With _web
.ContentType = "application/x-www-form-urlencoded"
.Method = "POST"
.KeepAlive = False
.ContentLength = _requestDocument.Length
.Timeout = 10000
'in case of proxyserver
.Proxy = New WebProxy("xxx.proxyserver.com", 8080)
'in case of required password
Dim cache As CredentialCache = New CredentialCache
cache.Add(New Uri("https://www.somedomain.com/"), "Basic", New NetworkCredential("Username", "password"))
.Credentials = cache
End With
send() 'send the request
retrieve() 'get the data
End Sub
End Class