Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HOW to solve ... System.Net.WebException: Connection closed

Reply
Thread Tools

HOW to solve ... System.Net.WebException: Connection closed

 
 
etantonio@gmail.com
Guest
Posts: n/a
 
      09-30-2005
Good morning, I've a problem, in the past I translate my site from
google or altavista with a code similar to this :

<%@ Page Language="c#" Trace="true" Debug="true" %>
<%@ import Namespace="System.Net" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
void Page_Load(Object Src, EventArgs E )
{
if (!Page.IsPostBack)
{
String sAddressTime =
"http://translate.google.com/translate?u=http%3A%2F%2Fwww.etantonio.it%2FIT%2FU niversita%2FMasterSatellitare%2F&langpair=it%7Cen& hl=en&ie=UTF-8&c2coff=1&ie=UTF-8&oe=UTF-8&prev=%2Flanguage_tools";

WebRequest req = WebRequest.Create(sAddressTime);
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
StreamReader reader = new StreamReader(ReceiveStream,
Encoding.ASCII);
String respHTML = reader.ReadToEnd();
Trace.Write("respHTML",respHTML);
}
}
</script>


Now this seems no more possible and the answer is

System.Net.WebException: Connection closed: Impossibile
impossible to connect to remote server

while if I directly insert the url in a browser

http://translate.google.com/translat...language_tools

I've the page correctly translated, there is a way to solve this my
problem ?? Many thanks.

Antonio

 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      09-30-2005
There are several things you can do to improve this, and get more
information out of it.

The first thing I would suggest is to avoid cross-posting to so many
newsgroups! Poor netiquette, and as likely to get you ignored as it is to
get you an answer. It p*sses people off.

Okay, now, first, you're using the wrong classes. You need to use
HttpWebRequest and HttpWebResponse. Example:

HttpWebRequest req = WebRequest.Create(sAddressTime);
HttpWebResponse result = req.GetResponse();

The HttpWebRequest and HttpWebResponse classes are derived from WebRequest
and WebResponse, but more specific to Http. In fact, when you use an HTTP
URL with WebRequest.Create, you get an HttpWebRequest back, and when you
send an HttpWebRequest, you get an HttpWebResponse back.

One of the first things you might want to do is check the Response. Here's a
little function I wrote that parses a string containing Response information
from an HttpWebResponse:

/// <summary>
/// Retrieves Response Information from the Response
/// </summary>
/// <param name="Response">Formatted string containing Response
Information</param>
/// <returns>Formatted string containing Response Information</returns>
public static string GetResponseInfo(HttpWebResponse Response)
{
try
{
StringBuilder sb = new StringBuilder(Response.ResponseUri.ToString());
sb.Append(Environment.NewLine + "Status Code: " +
Response.StatusCode.ToString());
sb.Append(Environment.NewLine + "Status Description: " +
Response.StatusDescription);
sb.Append(Environment.NewLine + "Content Length: " +
Response.ContentLength.ToString());
return sb.ToString();
}
catch (Exception ex)
{
//whatever you want to do to handle the exception
}
}

The second thing you might want to do is to examine any WebException that
comes back. The following method gets WebException details from a
WebException:

/// <summary>
/// Get Details of a WebException
/// </summary>
/// <param name="ex"><c>System.Net.WebException</c></param>
/// <returns>string containing WebException-specific details</returns>
private static string GetWebException(WebException ex)
{
string nl = Environment.NewLine;
StringBuilder sb = new StringBuilder();
sb.Append(nl + nl + ex.Message);
sb.Append(nl + ex.Response.ResponseUri.ToString());
sb.Append(nl + "Status: " + ex.Status.ToString());
return sb.ToString();
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.

<> wrote in message
news: ups.com...
> Good morning, I've a problem, in the past I translate my site from
> google or altavista with a code similar to this :
>
> <%@ Page Language="c#" Trace="true" Debug="true" %>
> <%@ import Namespace="System.Net" %>
> <%@ import Namespace="System.IO" %>
> <script runat="server">
> void Page_Load(Object Src, EventArgs E )
> {
> if (!Page.IsPostBack)
> {
> String sAddressTime =
> "http://translate.google.com/translate?u=http%3A%2F%2Fwww.etantonio.it%2FIT%2FU niversita%2FMasterSatellitare%2F&langpair=it%7Cen& hl=en&ie=UTF-8&c2coff=1&ie=UTF-8&oe=UTF-8&prev=%2Flanguage_tools";
>
> WebRequest req = WebRequest.Create(sAddressTime);
> WebResponse result = req.GetResponse();
> Stream ReceiveStream = result.GetResponseStream();
> StreamReader reader = new StreamReader(ReceiveStream,
> Encoding.ASCII);
> String respHTML = reader.ReadToEnd();
> Trace.Write("respHTML",respHTML);
> }
> }
> </script>
>
>
> Now this seems no more possible and the answer is
>
> System.Net.WebException: Connection closed: Impossibile
> impossible to connect to remote server
>
> while if I directly insert the url in a browser
>
> http://translate.google.com/translat...language_tools
>
> I've the page correctly translated, there is a way to solve this my
> problem ?? Many thanks.
>
> Antonio
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Established connection aborted and underlying connection closed Apu Nahasapeemapetilon ASP .Net Web Services 0 11-06-2006 04:24 PM
HOW to solve ... System.Net.WebException: Connection closed etantonio@gmail.com ASP .Net 1 09-30-2005 12:05 PM
Underlying connection was closed sumit ASP .Net 0 11-01-2003 11:28 AM
Opera - .closed not accessible if window is closed? Matt Kruse Javascript 5 09-09-2003 01:27 AM
"Connection closed by remote server" error in Opera BGS ASP .Net 0 07-24-2003 07:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57