HEAD 使用 WebClient?

发布于 2024-09-10 02:52:32 字数 72 浏览 4 评论 0原文

我假设答案是否定的,但是...... 有没有办法使用 WebClient 发送 HEAD 方法并将标头作为字符串或类似的内容返回?

I am going to assume the answer is no but....
Is there a way to use WebClient to send the HEAD method and return the headers as a string or something similar?

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

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

发布评论

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

评论(3

月朦胧 2024-09-17 02:52:32

你是对的,WebClient 不支持这一点。如果您想要此功能,可以使用 HttpWebRequest 并将方法设置为 HEAD:

System.Net.WebRequest request = System.Net.WebRequest.Create(uri);
request.Method = "HEAD";
request.GetResponse();

You are right WebClient does not support this. You can use HttpWebRequest and set the method to HEAD if you want this functionality:

System.Net.WebRequest request = System.Net.WebRequest.Create(uri);
request.Method = "HEAD";
request.GetResponse();
青萝楚歌 2024-09-17 02:52:32

另一种方法是继承 WebClient 并覆盖 GetWebRequest(Uri address )

    public class ExWebClient : WebClient
    {
        public string Method
        {
            get;
            set;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest webRequest = base.GetWebRequest(address);

            if (!string.IsNullOrEmpty(Method))
                webRequest.Method = Method;

            return webRequest;
        }
    }

Another way is to inherit from WebClient and override GetWebRequest(Uri address).

    public class ExWebClient : WebClient
    {
        public string Method
        {
            get;
            set;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest webRequest = base.GetWebRequest(address);

            if (!string.IsNullOrEmpty(Method))
                webRequest.Method = Method;

            return webRequest;
        }
    }
仅冇旳回忆 2024-09-17 02:52:32

我请求的大多数网络服务器都会接受此方法。但并非每个网络服务器都这样做。例如,IIS6 有时会遵循请求方法。

这是不允许某个方法时返回的状态代码...

catch (WebException webException)
            {
                    if (webException.Response != null)
                    {
                        //some webservers don't allow the HEAD method...
                        if (((HttpWebResponse) webException.Response).StatusCode == HttpStatusCode.MethodNotAllowed)

谢谢,
麦克风

Most web servers that I request from will accept this method. Not every web server does though. IIS6, for example, will honor the request method SOMETIMES.

This is the status code that is returned when a method isn't allowed...

catch (WebException webException)
            {
                    if (webException.Response != null)
                    {
                        //some webservers don't allow the HEAD method...
                        if (((HttpWebResponse) webException.Response).StatusCode == HttpStatusCode.MethodNotAllowed)

Thanks,
Mike

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文