473,389 Members | 1,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 software developers and data experts.

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

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
1 2205
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan Crowther | last post by:
On one PC the following test code results in an error on the GetResponse line while it works OK on another PC. The only difference that I am aware of is that the one that works is XP while the...
0
by: Kumar | last post by:
Hi all, I have the following code which uses WebClient.UploadValues myNameValueCollection.Add("Name", name) myNameValueCollection.Add("Age", age) .............. ............. Dim web As New...
5
by: Tim Reynolds | last post by:
My app calls a web service. After a few minutes of inactivity, we receive: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. A call...
1
by: Tim Reynolds | last post by:
Team, From a windows service, we consume a web service on another server and occasionally receive System.Net.WebException: The underlying connection was closed:. For some clients we call, we do...
8
by: Tim Reynolds | last post by:
Our .Net application calls a web method of aplpication 2 that resides on their Apache server. When I as a developer C#, Studios 2003, make the call to their web method from my desktop, I receive no...
0
by: Seth Thibodeaux | last post by:
I have built a Windows Service that connects to Sql Server 2000 Reporting Services via its Web Service interface for the purpose of generating reports. Intermittently, calls made to the Web Service...
2
by: zolof | last post by:
Hi, I am trying to make a simple file updload WinForm app. Here's the code: WebClient webClient = new WebClient(); webClient.UploadFile(new Uri("http://localhost/WebSite/Upload.aspx"),...
2
by: Scott McFadden | last post by:
When I invoke two web service methods sequentially with no delay, the first web method invocation goes smooth while the 2nd one generates the dredded: System.Net.WebException: The underlying...
0
by: DBC User | last post by:
Hello, One of my client reports they are getting "System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly." error suddently from the last week. None...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.