I have a small program that keep posting some data (4MB) to remote server. Each time when an internal 4MB buffer is full, I create a new HttpWebRequest, call BeginGetRequestStream/EndGetRequestStream, write data to stream, close it, and done. Because I don't care about response, so I didn't call GetResponse/BeginGetResponse/EndGetResponse.
My question is, since HttpWebRequest doesn't have a Close method, is it safe to leave current HttpWebRequest instance behind? If I didn't do anything to that HttpWebRequest, will the connection be automatically closed? If answer is no, how to explicitly close current HTTP connection? Thanks.
You shouldn't have to cleanup HttpWebRequest but you do need to close the stream coming back from GetRequestStream or the response from GetResponse. I know you said you aren't using any of the response methods, but you must be using one of them to actually make the request.
After you are finished with a WebResponse object, you must close it by calling the Close method. Alternatively, if you have gotten the response stream from the response object, you can close the stream by calling the Stream.Close method. If you do not close the response or the stream, your application can run out of connections to the server and become unable to process additional requests.[1]
[1] https://msdn.microsoft.com/en-us/library/debx8sh9.aspx
Related
I have an HTTPListener based .NET webserver, which generates data on request and outputs it. I am using StreamWriter to write to the output stream. When I write the data, not all of it gets received in Chrome, but all of it gets sent in the code. If I go to view source, it only shows part of the data that was sent.
It almost seems like some kind of timeout, or it getting too big, or something. Is there something that needs to be set in the HTTPListener or the request context?
As per the comments:
Call Flush on the stream before it is sent.
I want to navigate to my url, I don't need the response or anything.
HttpWebRequest request = WebRequestUtil.CreateHttpWebRequest(url);
I can log my navigation from server side and this does not navigate by itself, it just creates the request object.
I can navigate when I create a ResponseStream out of the request, but I don't need the response.
Is it possible to GET without calling the GetResponse method?
No, that is not possible. You need to request the response for the request to be sent.
You could build your own client to use the HTTP protocol directly, and make a special method that doesn't create a Stream object to handle the response stream, but the response stream itself always comes back from the server.
I couldn't find an exact example of what I'm looking for.
I have a REST/WCF endpoint that takes in a Stream and returns a Stream. I am able to do "live" streaming of the request, by that I mean that the incoming stream gets read by the service as it is written to by the client (using a shared buffer stream in the service).
I would like to achieve the same thing on the outgoing stream but all the examples I found write all the data to a local stream then return it back to the client.
How can I access the underlying stream of the response and start writing to it, allowing the client to start reading from it without exiting my service method?
Is it even possible to achieve what I need with WCF and HTTP streaming?
One of my colleague suggested me WebRequest.Create actually sends a web request to the specified url and I don't have to use asynchronous web request in order to avoid waiting.
Is this true?
How ever I don't agree with him and when I showed him the documentation but he said that he has been calling WebRequest.Create to send requests.
I'm not sure if this is true as i don't have deep knowledge of .net
No, calls to GetResponse() on the created WebRequest is what actually issues the request.
From the docs:
The GetResponse method sends a request to an Internet resource and returns a WebResponse instance.
That is not true. It just initializes a new request.
You need to call GetResponse() to actually make the call.
I am writing a program to crawl Web pages using C# HTTTWebRequest. Since I need to monitor the updates of a specific URL, I write the code as follows. However, I noticed that I could get responses for two times only. After that, it got no responses. Even though I lengthen the period to one minute, I still got the same problem. I don't understand why. Could you give me a hand? Thanks so much!
GreatFree
while (true)
{
WebRequest http = HttpWebRequest.Create("http://www.sina.com.cn");
HttpWebResponse response = (HttpWebResponse)http.GetResponse();
Console.WriteLine(response.LastModified);
Thread.Sleep(5000);
}
imaximchuk's answer is correct, but it doesn't explain why.
HTTP connections are pooled - one connection will be reused for a new request if it's available - and returning the connection to the pool occurs when you close or dispose the web response. In your case, you're not closing or disposing the response, so it's not being returned to the pool. There's a maximum number of connections to any particular host, which is configurable but usually shouldn't be changed. The default is two connections - which is why you're seeing two working responses and then a timeout.
I believe there's a finalizer somewhere which will release the connection when it notices the relevant object is eligible for garbage collection, but that's non-deterministic.
Basically: always close a web response, ideal using a using statement.
while (true)
{
WebRequest http = WebRequest.Create("http://www.sina.com.cn");
using (HttpWebResponse response = (HttpWebResponse)http.GetResponse())
{
Console.WriteLine(response.LastModified);
}
Thread.Sleep(5000);
}
You should call response.Close() when you have finishied working with respose or use using(HttpWebResponse response ... to close it automatically