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

Discussion in 'ASP .Net' started by etantonio@gmail.com, Sep 30, 2005.

  1. Guest

    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/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
     
    , Sep 30, 2005
    #1
    1. Advertising

  2. 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:...
    > 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/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
    >
     
    Kevin Spencer, Sep 30, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven Cheng[MSFT]
    Replies:
    4
    Views:
    1,662
    Steven Cheng[MSFT]
    Apr 8, 2004
  2. Steven Harris
    Replies:
    0
    Views:
    181
    Steven Harris
    Aug 14, 2003
  3. Jeremy Ames
    Replies:
    1
    Views:
    277
    Jeremy Ames
    Jan 8, 2004
  4. Edgar@CCrane
    Replies:
    3
    Views:
    214
    feroze [msft]
    Jul 1, 2004
  5. Rob Snell
    Replies:
    5
    Views:
    460
    Joel Zhou
    Sep 28, 2004
Loading...

Share This Page