Re: "The request was aborted: The connection was closed unexpectedly"

Discussion in 'ASP .Net' started by John Saunders, Jul 16, 2004.

  1. "Dave" <> wrote in message
    news:...
    > Hi,
    >
    > I'm trying to simulate a form post using the WebRequest class. But I

    can't find this error (see note below) documented on MSDN.
    >
    > string strId = "someid";
    > string strName = "somename";
    >
    > ASCIIEncoding encoding=new ASCIIEncoding();
    >
    > string postData="userid="+strId;
    > postData += ("&username="+strName);
    > byte[] data = encoding.GetBytes(postData);
    >
    > Prepare web request...
    > HttpWebRequest myRequest =

    (HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
    > myRequest.Method = "POST";
    > myRequest.ContentType="application/x-www-form-urlencoded";
    > myRequest.ContentLength = data.Length;
    >
    > Stream newStream=myRequest.GetRequestStream();
    > newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
    > newStream.Close();
    > WebResponse resp = myRequest.GetResponse();


    Throw a try block around that code and display the full exception in the
    catch. In addition, if it's a WebException, you may be able to display the
    Status property:

    try
    {
    string strId = "someid";
    string strName = "somename";

    ASCIIEncoding encoding=new ASCIIEncoding();

    string postData="userid="+strId;
    postData += ("&username="+strName);
    byte[] data = encoding.GetBytes(postData);

    Prepare web request...
    HttpWebRequest myRequest =
    (HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
    myRequest.Method = "POST";
    myRequest.ContentType="application/x-www-form-urlencoded";
    myRequest.ContentLength = data.Length;

    Stream newStream=myRequest.GetRequestStream();
    newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
    newStream.Close();
    WebResponse resp = myRequest.GetResponse();
    }
    catch (WebException wex)
    {
    Page.Trace.Write(wex.ToString());
    Page.Trace.Write(wex.Status);
    }
    catch (Exception ex)
    {
    Page.Trace.Write(ex.ToString());
    }
    --
    John Saunders
    johnwsaundersiii at hotmail
    John Saunders, Jul 16, 2004
    #1
    1. Advertising

  2. John Saunders

    Kyril Magnos Guest

    Re: "The request was aborted: The connection was closed unexpected

    What is the actual exception that is being thrown? That might help us figure
    out what is going on.
    --
    HTH

    Kyril Magnos

    Question of the day:
    What is Mono?
    A) Disease where the lymph nodes become swollen.
    B) A single sound
    C) A synonym for one
    D) A port of .NET meant to royally irritate MSFT
    E) All of the above.

    "Dave" <> wrote in message
    news:...
    | Thanks. I did all that but it doesn't really give me any insight as to
    why it's happening.
    |
    | "John Saunders" wrote:
    |
    | > "Dave" <> wrote in message
    | > news:...
    | > > Hi,
    | > >
    | > > I'm trying to simulate a form post using the WebRequest class. But I
    | > can't find this error (see note below) documented on MSDN.
    | > >
    | > > string strId = "someid";
    | > > string strName = "somename";
    | > >
    | > > ASCIIEncoding encoding=new ASCIIEncoding();
    | > >
    | > > string postData="userid="+strId;
    | > > postData += ("&username="+strName);
    | > > byte[] data = encoding.GetBytes(postData);
    | > >
    | > > Prepare web request...
    | > > HttpWebRequest myRequest =
    | >
    (HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
    | > > myRequest.Method = "POST";
    | > > myRequest.ContentType="application/x-www-form-urlencoded";
    | > > myRequest.ContentLength = data.Length;
    | > >
    | > > Stream newStream=myRequest.GetRequestStream();
    | > > newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
    | > > newStream.Close();
    | > > WebResponse resp = myRequest.GetResponse();
    | >
    | > Throw a try block around that code and display the full exception in the
    | > catch. In addition, if it's a WebException, you may be able to display
    the
    | > Status property:
    | >
    | > try
    | > {
    | > string strId = "someid";
    | > string strName = "somename";
    | >
    | > ASCIIEncoding encoding=new ASCIIEncoding();
    | >
    | > string postData="userid="+strId;
    | > postData += ("&username="+strName);
    | > byte[] data = encoding.GetBytes(postData);
    | >
    | > Prepare web request...
    | > HttpWebRequest myRequest =
    | >
    (HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
    | > myRequest.Method = "POST";
    | > myRequest.ContentType="application/x-www-form-urlencoded";
    | > myRequest.ContentLength = data.Length;
    | >
    | > Stream newStream=myRequest.GetRequestStream();
    | > newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
    | > newStream.Close();
    | > WebResponse resp = myRequest.GetResponse();
    | > }
    | > catch (WebException wex)
    | > {
    | > Page.Trace.Write(wex.ToString());
    | > Page.Trace.Write(wex.Status);
    | > }
    | > catch (Exception ex)
    | > {
    | > Page.Trace.Write(ex.ToString());
    | > }
    | > --
    | > John Saunders
    | > johnwsaundersiii at hotmail
    | >
    | >
    | >
    Kyril Magnos, Jul 16, 2004
    #2
    1. Advertising

  3. Re: "The request was aborted: The connection was closed unexpected

    Please provide the stack trace...

    --
    John Saunders
    johnwsaundersiii at hotmail


    "Dave" <> wrote in message
    news:...
    > Thanks. I did all that but it doesn't really give me any insight as to

    why it's happening.
    >
    > "John Saunders" wrote:
    >
    > > "Dave" <> wrote in message
    > > news:...
    > > > Hi,
    > > >
    > > > I'm trying to simulate a form post using the WebRequest class. But I

    > > can't find this error (see note below) documented on MSDN.
    > > >
    > > > string strId = "someid";
    > > > string strName = "somename";
    > > >
    > > > ASCIIEncoding encoding=new ASCIIEncoding();
    > > >
    > > > string postData="userid="+strId;
    > > > postData += ("&username="+strName);
    > > > byte[] data = encoding.GetBytes(postData);
    > > >
    > > > Prepare web request...
    > > > HttpWebRequest myRequest =

    > >

    (HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
    > > > myRequest.Method = "POST";
    > > > myRequest.ContentType="application/x-www-form-urlencoded";
    > > > myRequest.ContentLength = data.Length;
    > > >
    > > > Stream newStream=myRequest.GetRequestStream();
    > > > newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
    > > > newStream.Close();
    > > > WebResponse resp = myRequest.GetResponse();

    > >
    > > Throw a try block around that code and display the full exception in the
    > > catch. In addition, if it's a WebException, you may be able to display

    the
    > > Status property:
    > >
    > > try
    > > {
    > > string strId = "someid";
    > > string strName = "somename";
    > >
    > > ASCIIEncoding encoding=new ASCIIEncoding();
    > >
    > > string postData="userid="+strId;
    > > postData += ("&username="+strName);
    > > byte[] data = encoding.GetBytes(postData);
    > >
    > > Prepare web request...
    > > HttpWebRequest myRequest =
    > >

    (HttpWebRequest)WebRequest.Create("https://secure.somecontentwepayfor.com");
    > > myRequest.Method = "POST";
    > > myRequest.ContentType="application/x-www-form-urlencoded";
    > > myRequest.ContentLength = data.Length;
    > >
    > > Stream newStream=myRequest.GetRequestStream();
    > > newStream.Write(data,0,data.Length); <---**ERROR OCCURS HERE**.
    > > newStream.Close();
    > > WebResponse resp = myRequest.GetResponse();
    > > }
    > > catch (WebException wex)
    > > {
    > > Page.Trace.Write(wex.ToString());
    > > Page.Trace.Write(wex.Status);
    > > }
    > > catch (Exception ex)
    > > {
    > > Page.Trace.Write(ex.ToString());
    > > }
    > > --
    > > John Saunders
    > > johnwsaundersiii at hotmail
    > >
    > >
    > >
    John Saunders, Jul 16, 2004
    #3
    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.

Share This Page