Reusing HttpWebRequest.GetRequestStream

D

Dan

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
 
G

Guest

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
 
D

Dan

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 said:
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".
 
J

Joerg Jooss

Dan said:
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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top