When writing the below my code locks up on GetResponse. Why?
try
{
WebRequest myWebRequest = WebRequest.Create(strURL);
WebResponse myWebResponse = myWebRequest.GetResponse();
//more code here
When writing the below my code locks up on GetResponse. Why?
try
{
WebRequest myWebRequest = WebRequest.Create(strURL);
WebResponse myWebResponse = myWebRequest.GetResponse();
//more code here
This usually happens if you've made several requests to the same host, and not disposed of the WebResponse
.
The default connection management settings only allow 2 (or maybe 4, I can't remember) open connections to the same host at a time. If you really need to change this, use the <connectionManagement>
app.config element - but usually you'll be fine just disposing of WebResponse
:
try
{
WebRequest myWebRequest = WebRequest.Create(strURL);
using (WebResponse myWebResponse = myWebRequest.GetResponse())
{
//more code here
myWebResponse.GetResponseStream()
, will it not Close()
the stream before the entire stream is returned?... if I do not use the using
statement,won't that that also mean that myWebResponse
will not be properly disposed?... I know I can close the Stream
at my calling function, but not sure what happens to the WebResponse
object –
Peraea Stream
parameters, one incoming and the other being written to, as per convention for streams. Disposes nicely! –
Peraea © 2022 - 2024 — McMap. All rights reserved.