如何用C#只请求HTTP头?
我想检查一个大文件的 URL 是否存在。我正在使用下面的代码,但它太慢了:
public static bool TryGet(string url)
{
try
{
GetHttpResponseHeaders(url);
return true;
}
catch (WebException)
{
}
return false;
}
public static Dictionary<string, string> GetHttpResponseHeaders(string url)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
WebRequest webRequest = HttpWebRequest.Create(url);
using (WebResponse webResponse = webRequest.GetResponse())
{
foreach (string header in webResponse.Headers)
{
headers.Add(header, webResponse.Headers[header]);
}
}
return headers;
}
I want to check if the URL of a large file exists. I'm using the code below but it is too slow:
public static bool TryGet(string url)
{
try
{
GetHttpResponseHeaders(url);
return true;
}
catch (WebException)
{
}
return false;
}
public static Dictionary<string, string> GetHttpResponseHeaders(string url)
{
Dictionary<string, string> headers = new Dictionary<string, string>();
WebRequest webRequest = HttpWebRequest.Create(url);
using (WebResponse webResponse = webRequest.GetResponse())
{
foreach (string header in webResponse.Headers)
{
headers.Add(header, webResponse.Headers[header]);
}
}
return headers;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置:
这样服务器将仅响应标头信息(无内容)。这对于检查服务器是否接受某些操作(即压缩数据等)也很有用。
You need to set:
This way the server will respond with the header information only (no content). This is also useful to check if the server accepts certain operations (i.e. compressed data etc.).