HttpWebRequest.GetRequestStream Causes an Exception

J

Jason

I'm having a hard time getting a call to HttpWebRequest's
GetRequestSteam to work. Each time I try to run it, I get the
following error:


The underlying connection was closed: Unable to connect to the
remote server.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The underlying
connection was closed: Unable to connect to the remote server.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location
of the exception can be identified using the exception stack trace
below.

Stack Trace:


[WebException: The underlying connection was closed: Unable to
connect to the remote server.]
System.Net.HttpWebRequest.CheckFinalStatus() +677
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult
asyncResult) +75
System.Net.HttpWebRequest.GetRequestStream() +233
ERSI.Store.UPSShipping.RequestShipping() +113
...


The code I use works perfectly on both my development machine and on
another webserver where we host our main website. However, when I try
to run this on a our internal company web server (running Microsoft
Small Business Server, ISA Firewall, and IIS 6), it seems to timeout
and give me the error above.

The following code produces the error:


string szResponse = "";
Uri url = new Uri("https://www.ups.com/ups.app/xml/Rate");
HttpWebRequest connection = (HttpWebRequest)WebRequest.Create(url);
connection.UnsafeAuthenticatedConnectionSharing = true;
connection.Method = "POST";
Stream output = connection.GetRequestStream(); // <-- Error Thrown

string szXML = GetRequestString();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] request = encoding.GetBytes(szXML);
output.Write(request, 0, request.Length);
output.Close();

HttpWebResponse resp = (HttpWebResponse)connection.GetResponse();
StreamReader strStream = new StreamReader(resp.GetResponseStream(),
Encoding.ASCII);

try {
szResponse = strStream.ReadToEnd();
} catch (Exception exc) {
System.Diagnostics.Debug.WriteLine(exc.StackTrace);
}

resp.Close();


Because it works great on both our development machines and our main
websites, I don't think that the problem is with the code, but rather
some setting in ISA or SBS. I would appreciate any suggestions on how
to fix the problem or even an alternate newsgroup to try if nobody has
any ideas. Thanks.
 
A

Alvin Bruney [ASP.NET MVP]

see if you can ping the url/domain from the webserver or launch a browser on
the server and see if you can hit the url. usually, this type of exception
gets thrown when the server can't connect to the url

--
Regards,
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc
_________________________


Jason said:
I'm having a hard time getting a call to HttpWebRequest's
GetRequestSteam to work. Each time I try to run it, I get the
following error:


The underlying connection was closed: Unable to connect to the
remote server.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The underlying
connection was closed: Unable to connect to the remote server.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location
of the exception can be identified using the exception stack trace
below.

Stack Trace:


[WebException: The underlying connection was closed: Unable to
connect to the remote server.]
System.Net.HttpWebRequest.CheckFinalStatus() +677
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult
asyncResult) +75
System.Net.HttpWebRequest.GetRequestStream() +233
ERSI.Store.UPSShipping.RequestShipping() +113
...


The code I use works perfectly on both my development machine and on
another webserver where we host our main website. However, when I try
to run this on a our internal company web server (running Microsoft
Small Business Server, ISA Firewall, and IIS 6), it seems to timeout
and give me the error above.

The following code produces the error:


string szResponse = "";
Uri url = new Uri("https://www.ups.com/ups.app/xml/Rate");
HttpWebRequest connection = (HttpWebRequest)WebRequest.Create(url);
connection.UnsafeAuthenticatedConnectionSharing = true;
connection.Method = "POST";
Stream output = connection.GetRequestStream(); // <-- Error Thrown

string szXML = GetRequestString();
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] request = encoding.GetBytes(szXML);
output.Write(request, 0, request.Length);
output.Close();

HttpWebResponse resp = (HttpWebResponse)connection.GetResponse();
StreamReader strStream = new StreamReader(resp.GetResponseStream(),
Encoding.ASCII);

try {
szResponse = strStream.ReadToEnd();
} catch (Exception exc) {
System.Diagnostics.Debug.WriteLine(exc.StackTrace);
}

resp.Close();


Because it works great on both our development machines and our main
websites, I don't think that the problem is with the code, but rather
some setting in ISA or SBS. I would appreciate any suggestions on how
to fix the problem or even an alternate newsgroup to try if nobody has
any ideas. Thanks.
 
J

Jason

I get no response on the webserver. It connects and returns some basic
information on my own computer.
 
J

Jason

I case anyone is interested or is having a similar problem, I managed
to track the problem down. I did a side-by-side comparison of the ISA
settings on the webserver that working and the one that wasn't. After
try a variety of different settings, I noticed two packet filters that
were missing on the problem machine.

Under Access Policy -> IP Packet Filtering, I added the following
filters:

- SSL Access
IP Protocol: TCP
Direction: Outbound
Local Port: All ports
Remote port: Fixed port
Remote port number: 443
Applies to: Default IP Address(es) on the external interface(s)
Applies to: All remote computers

- Web Access
IP Protocol: TCP
Direction: Outbound
Local Port: All ports
Remote port: Fixed port
Remote port number: 80
Applies to: Default IP Address(es) on the external interface(s)
Applies to: All remote computers

Apparently, these filters were created on our other servers by default
(or by a different administrator), but for some reason were not on this
one. Probably something an experienced network administrator would
have picked up on immediately, but hopefully this will help any novice
administrators who happen to run into a similar problem.
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top