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

E

etantonio

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/transla...coff=1&ie=UTF-8&oe=UTF-8&prev=/language_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/transla...coff=1&ie=UTF-8&oe=UTF-8&prev=/language_tools

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

Antonio
 
K

Kevin Spencer

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.
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top