|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
HOW to solve ... System.Net.WebException: Connection closedgoogle 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%2FUniversita%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/translate?u=http%3A%2F%2Fwww.etantonio.it%2FIT%2FUniversita%2FMasterSatellitare%2F&langpair=it%7Cen&hl=en&ie=UTF-8&c2coff=1&ie=UTF-8&oe=UTF-8&prev=%2Flanguage_tools I've the page correctly translated, there is a way to solve this my problem ?? Many thanks. Antonio 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(); } -- Show quoteHide quoteHTH, Kevin Spencer Microsoft MVP ..Net Developer Big things are made up of lots of little things. <etanto***@gmail.com> wrote in message news:1128062639.230682.60820@g14g2000cwa.googlegroups.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%2FUniversita%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/translate?u=http%3A%2F%2Fwww.etantonio.it%2FIT%2FUniversita%2FMasterSatellitare%2F&langpair=it%7Cen&hl=en&ie=UTF-8&c2coff=1&ie=UTF-8&oe=UTF-8&prev=%2Flanguage_tools > > I've the page correctly translated, there is a way to solve this my > problem ?? Many thanks. > > Antonio >
Other interesting topics
Beginning C# Q
is there any way to clear the buffer of a System.IO.StreamWriter so that it does not do a flush when Supported OS Socket.BeginSend and EndSend causes thread count to runaway Windows Service and shutdown/restart of server... VS 2005 RC: "has encountered a problem" on deployment Manifest Resources: format Resources files Not all images are loaded correctly ClickOnce Deployment Manifest Issues |
|||||||||||||||||||||||