Does WebRequest.Create method actually calls a url? - c#

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.

Related

Does HttpWebRequest need to be closed? How

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

The purpose of HTTPWebRequest and HTTPWebResponse

I'm doing research into web programming in ASP .NET and came across these two classes. I was wondering what might these be used for?
My first thought is that they could be used if you were setting up a proxy between the client and server, but I'm not sure if this is the main purpose or not.
Thanks
edit: classes not methods
They can indeed be used for that. This isn't specific to ASP.NET however.
You can create a HttpWebRequest object by doing:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://url.com");
And you invoke it to retrieve an HttpWebResponse:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
There's a lot of customization you can do here, but hopefully this will give you a starting point for reading data off the web.
As their names imply, these are classes to either create a request (HttpWebRequest) or create a response (HttpWebResponse).
Using HttpWebRequest you define a request to a URI using the HTTP-protocol. Whereas the HttpWebResponse class delivers you an answer of an HTTP-server providing all the information like HTTP-headers and actual body of the request.
This is an example from MSDN
HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
// Insert code that uses the response object.
HttpWResp.Close();
Consider the casting from the base-class WebRequest.
See HttpWebRequest and HttpWebResponse.
They are used for communicating with another process using the HTTP protocol.
In the context of ASP.NET, your process could use them to talk to another service. Maybe your database uses the HTTP protocol, such as CouchDB. Perhaps you have a rest service that your ASP.NET application needs to talk to.

Just navigating to a page with HttpWebRequest

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.

Parallel HTTPRequest using HttpClient in Windows 8 app

I am using HttpClient for sending HTTP requests and receiving HTTP responses in my Windows 8 app. I have few questions on the same:
1) Can I send multiple/parallel HTTP requests using a single HttpClient object? Is there a recommended way to use HttpClient object efficiently?
2) What is the difference when I create HttpClient object every time and when I re-use the same object for each new request?
3) I am tracking the requests and responses using Fiddler. What I found out is that the response time in Fiddler is different than the response time I am calculating manually inside my App. The response time for a request in Fiddler is always lower than the calculated response time in my app. Can anybody please tell me why it is like that?
4) One more thing I came across is that for every request it is doing HTTPS handshake. Instead it should do it only first time. I checked it using Fiddler and it is clearly visible there. Is there any property I need to set in HttpClient object to stop this from doing it every time.
5) Whether HttpClient is thread-safe?
1 & 5:
HttpClient manual:
The following methods are thread safe:
CancelPendingRequests
DeleteAsync
GetAsync
GetByteArrayAsync
GetStreamAsync
GetStringAsync
PostAsync
PutAsync
SendAsync
2 & 4:
HttpClient manual:
The HttpClient class instance acts as a session to send HTTP requests.
3:
Fiddler acts as a proxy. Your browser sends the request to Fiddler, which forwards it to the origin server. This adds upon the request time.
Make sure that you use the same HttpClient object for each async HttpRequest which will prevent it from overlapping the requests

C# HTTPWebRequest Cannot Be Used to Access a URL Periodically?

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

Categories