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,
 
Joined
Jun 1, 2007
Messages
1
Reaction score
0
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:

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top