tags:

views:

246

answers:

2

The HttpWebRequest class appears to be a rather heavy and functionality loaded.

I only need to (as quickly and low-overhead as possible) grab a response from a Url, without any other fancy functionality. What is the fastest method with the lowest overhead to achieve this?

Thank you!

A: 

Have you done any benchmarking to verify using WebRequest is not what you need, and pinpointed the particular characteristics of WebRequest that make it so? WebRequest makes an HTTP request to the URI and gets the response. Any other functionality in the WebRequest class might be present, but if you don't actually use it, it's not hurting anything to be there. Are you actually running out of resources and WebRequest is the culprit?

In response to the comments, you said you are using WebClient - that is considerably more substantial than using WebRequest.

Rex M
I just noticed the quite large concentration of time on instantiating the Web request vs. the rest of the code of the controller method and wondered if I can do some quick (premature) optimization here with changing to another object. I guess there isn't an alternative tough!
Alex
+1  A: 

You could do

string url = "www.example.com";
IPAddress[] addresses = Dns.GetHostAddresses(url);

To look up the IP, then use a TCP-client to send your own minimized custom http request to the server, resulting in a pure stream-based response. Doesn't get any lower overhead than that :)

BTW; if you don't need the full response, but just need to check some basic info, look into the HTTP HEAD-requesttype. Wikipedia.

cwap
Quite creative :) No I do need the actual response content. I guess HttpWebRequest is already as good as it gets then..
Alex