Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebRequest method HEAD returns body

My web request with the method "Head" keeps returning the body of my webpage (on localhost). Here is how it is basically created:

HttpWebRequest webrequest = WebRequest.Create(url.ToString()) as HttpWebRequest;
webrequest.Method = "HEAD";
WebResponse response = webrequest.GetResponse();

As I put a breakpoint in my aspx.cs page, I step into the OnInit() method and also the Page_Load() method where I believe I'm not supposed to step in with a Head method request (am I wrong?).

In my Page_Load() I execute some code that I do not want to be executed when I call with the Head method, but later when I call with the Get method (once I got the headers).

Am I missing something? (not too familiar with Http requests and responses yet... :/) Thank you for your help!

like image 420
Slyvain Avatar asked Jul 15 '26 14:07

Slyvain


1 Answers

Try this sample code approach ....

for (int i = 0; i < ParsedLinks.Count; i++)
{
        Thread.Sleep(500);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(ParsedLinks[i]);
        req.Method = "HEAD";
        req.Credentials = CredentialCache.DefaultCredentials;
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        bool b_Result = int.TryParse(resp.Headers.Get("Content-Length"), out i_ContentLength);
        int i_Size = (int)(i_ContentLength / 1024);
        req.Abort();
        resp.Close();

}

hope it helps http://forums.asp.net/t/1412824.aspx/1

like image 90
Madhu Beela Avatar answered Jul 18 '26 04:07

Madhu Beela



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!