HttpWebRequest 方法 HEAD 返回正文

发布于 2024-12-28 13:24:06 字数 484 浏览 0 评论 0原文

我使用“Head”方法的网络请求不断返回我的网页正文(在本地主机上)。 下面是它的基本创建方式:

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

当我在 aspx.cs 页面中放置一个断点时,我进入 OnInit() 方法以及 Page_Load() 方法,我认为我不应该使用 Head 方法进入该方法请求(我错了吗?)。

在我的 Page_Load() 中,我执行了一些代码,当我使用 Head 方法调用时,我不希望执行这些代码,但稍后当我使用 Get 方法调用时(一旦获得标头),就会执行这些代码。

我错过了什么吗? (还不太熟悉 Http 请求和响应...:/) 感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美羊羊 2025-01-04 13:24:06

尝试这个示例代码方法......

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();

}

希望它有帮助
http://forums.asp.net/t/1412824.aspx/1

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

美胚控场 2025-01-04 13:24:06

不同之处在于,如果您尝试读取响应,则在 HEAD 的情况下您什么也得不到。在哪里可以看到 GET 情况下的响应内容

            var response = webrequest.GetResponse().GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            StreamReader readStream = new StreamReader(response, encode);
            Console.WriteLine(readStream.ReadToEnd().Length) // you should see 0
            readStream.Close();

The difference is, if you try to read the response you get nothing in case of HEAD. Where as you can see the response content in case of GET

            var response = webrequest.GetResponse().GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            StreamReader readStream = new StreamReader(response, encode);
            Console.WriteLine(readStream.ReadToEnd().Length) // you should see 0
            readStream.Close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文