A
Adrian D. Garcia
Hi,
I m trying to connect to a HTTP Server using the HTTPWebRequest Class but
I'm getting the following error:
System.Net.WebException: The underlying connection was closed: Unable to
connect to the remote server. ---> System.Net.WebException: The underlying
connection was closed: Unable to connect to the remote server.
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.HttpWebRequest.GetResponse()
at System.Net.Connection.TunnelThroughProxy(Uri proxy, HttpWebRequest
originalRequest, Socket& socket)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.HttpWebRequest.GetResponse()
at Proceso.Proceso.MiRequest(String Method, String URL, String
DataToPost, NameValueCollection RequestHeaders, NameValueCollection&
ResponseHeaders, String URLReferer, String ContentType, String& ResponseURL)
in E:\_TF\Proceso.vb:line 67
Here is the code that generate the problem:
Public Function MiRequest(ByVal Method As String, ByVal URL As String
String, ByVal ContentType As String) As String
Dim i As Integer
Dim res As HttpWebResponse
Dim req As HttpWebRequest
Dim RequestStream As Stream
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
req = WebRequest.Create(URL)
req.Method = Method
'req.Timeout = 12000
req.KeepAlive = False
req.ContentType = ContentType
Dim SomeBytes() As Byte
Dim UrlEncoded As New StringBuilder
Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}
res = req.GetResponse()
ReceiveStream = res.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)
Dim buffer As New System.Text.StringBuilder(1000)
Do While count > 0
buffer.Append(read, 0, count)
count = sr.Read(read, 0, 256)
Loop
res.Close()
Return buffer.ToString()
End Function
As you can see I m using the KeepAlive property to False. (KB Suggestion).
This code works fine inside an ASP.NET page but always generate the prvius
error when I used inside a Console App or Windows Form App.
The O.S. is Windows XP with SP 1 with Framework 1.1 installed.
Any idea why its works inside an ASP.NET process but not in other process?
Regards.
Adrian
I m trying to connect to a HTTP Server using the HTTPWebRequest Class but
I'm getting the following error:
System.Net.WebException: The underlying connection was closed: Unable to
connect to the remote server. ---> System.Net.WebException: The underlying
connection was closed: Unable to connect to the remote server.
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.HttpWebRequest.GetResponse()
at System.Net.Connection.TunnelThroughProxy(Uri proxy, HttpWebRequest
originalRequest, Socket& socket)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.CheckFinalStatus()
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.HttpWebRequest.GetResponse()
at Proceso.Proceso.MiRequest(String Method, String URL, String
DataToPost, NameValueCollection RequestHeaders, NameValueCollection&
ResponseHeaders, String URLReferer, String ContentType, String& ResponseURL)
in E:\_TF\Proceso.vb:line 67
Here is the code that generate the problem:
Public Function MiRequest(ByVal Method As String, ByVal URL As String
String, ByVal ContentType As String) As String
Dim i As Integer
Dim res As HttpWebResponse
Dim req As HttpWebRequest
Dim RequestStream As Stream
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
req = WebRequest.Create(URL)
req.Method = Method
'req.Timeout = 12000
req.KeepAlive = False
req.ContentType = ContentType
Dim SomeBytes() As Byte
Dim UrlEncoded As New StringBuilder
Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}
res = req.GetResponse()
ReceiveStream = res.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)
Dim buffer As New System.Text.StringBuilder(1000)
Do While count > 0
buffer.Append(read, 0, count)
count = sr.Read(read, 0, 256)
Loop
res.Close()
Return buffer.ToString()
End Function
As you can see I m using the KeepAlive property to False. (KB Suggestion).
This code works fine inside an ASP.NET page but always generate the prvius
error when I used inside a Console App or Windows Form App.
The O.S. is Windows XP with SP 1 with Framework 1.1 installed.
Any idea why its works inside an ASP.NET process but not in other process?
Regards.
Adrian