Reusing HttpWebRequest.GetRequestStream

Discussion in 'ASP .Net' started by Dan, Feb 2, 2004.

  1. Dan

    Dan Guest

    I'm experimenting with HttpWebRequest. After I've created a request:

    m_request = (HttpWebRequest) WebRequest.Create(uri);

    I'd like to repeatedly send requests to the URI, as follows:

    public void SendRequest(string body)
    {
    StreamWriter stream = new
    StreamWriter(m_request.GetRequestStream(),Encoding.ASCII);
    stream.Write(body);
    stream.Close();
    }

    However, the second time I send the request I get the following exception:

    An unhandled exception of type 'System.ArgumentException' occurred in
    mscorlib.dll
    Additional information: Stream was not writable.

    Does anybody know why that happens?
    Thanks...

    Dan
     
    Dan, Feb 2, 2004
    #1
    1. Advertising

  2. Dan
    That is part of the HTTP protocol. After a Web request is made, the underlying connection is closed unless the client specifies to keep it open for a period of time. You can request the connection to stay open by setting the Connection property of the HttpWebRequest object to "Keep-alive"

    Tu-Thac

    ----- Dan wrote: ----


    I'm experimenting with HttpWebRequest. After I've created a request

    m_request = (HttpWebRequest) WebRequest.Create(uri)

    I'd like to repeatedly send requests to the URI, as follows

    public void SendRequest(string body

    StreamWriter stream = ne
    StreamWriter(m_request.GetRequestStream(),Encoding.ASCII)
    stream.Write(body)
    stream.Close()


    However, the second time I send the request I get the following exception

    An unhandled exception of type 'System.ArgumentException' occurred i
    mscorlib.dl
    Additional information: Stream was not writable

    Does anybody know why that happens
    Thanks..

    Da
     
    =?Utf-8?B?VHUtVGhhY2g=?=, Feb 2, 2004
    #2
    1. Advertising

  3. Dan

    Dan Guest

    Thanks. However, when I set the connection property to "Keep-Alive", I got
    the following exception: "Keep-Alive and Close may not be set with this
    property."

    I also tried setting the KeepAlive property to true, which made no
    difference: I still got the "Stream was not writable" exception.



    "Tu-Thach" <> wrote in message
    news:...
    > Dan,
    > That is part of the HTTP protocol. After a Web request is made, the

    underlying connection is closed unless the client specifies to keep it open
    for a period of time. You can request the connection to stay open by
    setting the Connection property of the HttpWebRequest object to
    "Keep-alive".
    >
    > Tu-Thach
    >
    > ----- Dan wrote: -----
    >
    >
    > I'm experimenting with HttpWebRequest. After I've created a request:
    >
    > m_request = (HttpWebRequest) WebRequest.Create(uri);
    >
    > I'd like to repeatedly send requests to the URI, as follows:
    >
    > public void SendRequest(string body)
    > {
    > StreamWriter stream = new
    > StreamWriter(m_request.GetRequestStream(),Encoding.ASCII);
    > stream.Write(body);
    > stream.Close();
    > }
    >
    > However, the second time I send the request I get the following

    exception:
    >
    > An unhandled exception of type 'System.ArgumentException' occurred in
    > mscorlib.dll
    > Additional information: Stream was not writable.
    >
    > Does anybody know why that happens?
    > Thanks...
    >
    > Dan
    >
    >
    >
     
    Dan, Feb 2, 2004
    #3
  4. Dan

    Joerg Jooss Guest

    Dan wrote:
    > I'm experimenting with HttpWebRequest. After I've created a request:
    >
    > m_request = (HttpWebRequest) WebRequest.Create(uri);
    >
    > I'd like to repeatedly send requests to the URI, as follows:
    >
    > public void SendRequest(string body)
    > {
    > StreamWriter stream = new
    > StreamWriter(m_request.GetRequestStream(),Encoding.ASCII);
    > stream.Write(body);
    > stream.Close();
    > }
    >
    > However, the second time I send the request I get the following
    > exception:
    >
    > An unhandled exception of type 'System.ArgumentException' occurred in
    > mscorlib.dll
    > Additional information: Stream was not writable.


    I don't think its possible to reuse the request stream. Instead, just create
    a new (Http)WebRequest instance.

    Cheers,
    --
    Joerg Jooss
     
    Joerg Jooss, Feb 7, 2004
    #4
  5. Dan

    taylorjonl

    Joined:
    Jun 1, 2007
    Messages:
    1
    For anyone browsing this after the 3 years it was posted, the original poster was closing the stream with this line.

    Code:
    stream.Close();
    Once a stream is closed the connection is severed. If you want to use the KeepAlive feature from HttpWebRequest, just reuse the same HttpWebRequest object, just call GetResponse() after each time and it will reset the request internally so the next time you get the the request stream that is a second Http request.
     
    Last edited: Jun 1, 2007
    taylorjonl, Jun 1, 2007
    #5
    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. Jason
    Replies:
    3
    Views:
    13,806
    Jason
    Mar 17, 2005
  2. Replies:
    0
    Views:
    830
  3. =?Utf-8?B?U21pbGluZ0pvZTg4?=

    Getting error 403 unauthorized for HttpWebRequest.GetRequestStream

    =?Utf-8?B?U21pbGluZ0pvZTg4?=, Jul 21, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,889
    Joerg Jooss
    Jul 25, 2006
  4. Dmitriy

    HttpWebRequest::GetRequestStream gets blocked

    Dmitriy, Jun 9, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    607
    Michelle Hlaing
    Feb 25, 2005
  5. Edgile
    Replies:
    4
    Views:
    4,092
    Kevin
    Oct 3, 2008
Loading...

Share This Page