By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

<et*******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro 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

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.