System.Net.WebException

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
 
F

Feroze [msft]

From the stack, it looks as if the app is trying to connect through proxy
and failing,

does your network have a http proxy that you need to use ? if so, set the
proxy address in a WebProxy instance, and set the "proxy" property of the
HttpWebRequest object.

--
feroze

-----------------
This posting is provided as-is. It offers no warranties and assigns no
rights.

See http://weblogs.asp.net/feroze_daud for System.Net related posts.
----------------
 
A

Adrian D. Garcia

Thanks for the replay,

My network hasnt a proxy buy I m working with the idea....
Where I can configure the default proxy to the stack that .NET use to
connect to HTTP? The machine is a Windows XP Prof. with SP1. I was traiyng
with proxycfg but without luck.
Also I m looking at machine.config, at the systen.net section.
Why do you think that the ASP.NET app can connect and the conolse/windforns
apps dont? Wich are the difference between this type of app?

Thanks in advance
 
A

Adrian D. Garcia

Thanks for your response. It let me found the solution.

Selecting a "no proxy" resolved the problem

req.Proxy = GlobalProxySelection.GetEmptyWebProxy()

But I still have some doubts about from where the HTTP stacks take the
information to set the default proxy.
Reading the docs aboyt de <defaultProxy> section in the configuration file I
found the following:


<proxy> Element
See Also
<defaultProxy> Element | Network Settings Schema

Defines a proxy server.

<configuration>
<system.net>
<defaultProxy>
<proxy>

<proxy
usesystemdefault = "false | true"
bypassonlocal = "true | false"
proxyaddress = "proxy address including port"
/>Optional Attributes
Attribute Description
usesystemdefault Indicates that the application should use the default
system proxy.
bypassonlocal Indicates that local addresses should not use a proxy.
proxyaddress The URI or IP address of the Internet proxy.

Remarks
The <proxy> element defines a proxy server for an application. When the
usesystemdefault attribute is true, the application uses the proxy defined
in the Internet Options dialog box.

When the bypassonlocal property is true, local addresses do not use the
proxy. A local address has no period; is localhost, loopback, or 127.0.0.1;
or matches the machine name of the local computer.

........................

But I tried this configuration in my App.Config file
<system.net>

<defaultProxy>

<proxy usesystemdefault="true"/>

</defaultProxy>

</system.net>

to het the proxy configuration from the IE but still it hasnt worked.

--

Regards

Adrian D. Garcia
MCSD
NDSoft Consultoria y Desarrollo
 

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