WebRequest的" HEAD"重量轻替代 [英] WebRequest "HEAD" light weight alternative

查看:184
本文介绍了WebRequest的" HEAD"重量轻替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我发现以下不与某些网站,如IMDB.com工作。

I recently discovered that the following does not work with certain sites, such as IMDB.com.

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                System.Net.WebRequest wc = System.Net.WebRequest.Create("http://www.imdb.com"); //args[0]);

                ((HttpWebRequest)wc).UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/0.2.153.1 Safari/525.19";
                wc.Timeout = 1000;
                wc.Method = "HEAD";
                WebResponse res = wc.GetResponse();
                var streamReader = new System.IO.StreamReader(res.GetResponseStream());

                Console.WriteLine(streamReader.ReadToEnd());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

它返回一个HTTP 405(不允许的方法)。我的问题是,我用code非常相似,上面来检查链接是否有效和时代的绝大多数它工作正常。我可以切换到方法等于GET和它的工作原理(与增加超时),但是这会减慢速度由一个数​​量级。我假设的405响应服务器配置上IMDB的服务器端。

It returns an HTTP 405 ( Method Not Allowed ). My problem is, I use code very similar to the above to check if a link is valid and the vast majority of times it works correctly. I can switch it to method equal GET and it works ( with an increase in timeout ), but this slows things down by an order of magnitude. I am assuming the 405 response is a server configuration on IMDB's server side.

有没有办法为我做同样的事情上面,在重量轻的方式。NET?或者,有没有办法来解决上述code所以它的工作原理与IMDB的工作原理GET请求?

Is there a way for me to do the same thing as above, in a light weight manner in .NET? Or, is there a way to fix the above code so it works as a GET request that works with imdb?

推荐答案

您将不得不澄清你所说的轻量级的意思。什么是你要完成?

You'll have to clarify what you mean by "lightweight". What are you trying to accomplish?

无论你是否可以使用GET / POST /头/删除/等将取决于URL和什么配置。可以在服务器上的URL运行应用程序。

Whether or not you can use GET/POST/HEAD/DELETE/etc will depend on the URL and what's configured in the application that is running on the server at that URL.

如果你正在试图做的是看你是否能建立连接,而无需实际下载的内容,您也许可以尝试只启动使用的插座的,但没有真正可靠的或普遍支持的方式只是通过改变HTTP方法。

If all you're trying to do is see if you can make a connection without actually downloading the content you could maybe try just initiating a connection to port 80 using sockets, but there isn't really reliable or universally supported way just by changing the HTTP method.

这篇关于WebRequest的" HEAD"重量轻替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆