C#中获取内容类型的响应,而无需加载全部内容 [英] C# Get content type of a response without loading entire content

查看:161
本文介绍了C#中获取内容类型的响应,而无需加载全部内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要确保从那些谁知道,如果下面的代码行只会请求的响应类型,而无需加载的全部内容,因为我必须在我的应用程序的一些条件在响应类型,我不想浪费时间应该被忽略的资源。

 要求=(HttpWebRequest的)System.Net.HttpWebRequest.Create(URL) ; 
响应=(HttpWebResponse)request.GetResponse();
变种的responseType = response.ContentType;


解决方案

这可能是最容易设置的 方法属性HEAD - 这样你不会得到实际的内容,就在标题



在另一方面,这并不意味着你需要做出两个请求,当你的的想要的内容...


I want to make sure from those who know if the following lines of code will only get the response type of the request without loading the full content, because i have to to some conditions over response type in my application and i dont want to waste time for that resources that should be ignored.

request = (HttpWebRequest)System.Net.HttpWebRequest.Create(url);
response = (HttpWebResponse)request.GetResponse();
var responseType = response.ContentType;

解决方案

It's probably easiest to set the Method property to "HEAD" - that way you won't get the actual contents, just the headers.

On the other hand, that does mean you'll need to make two requests when you do want the contents...

这篇关于C#中获取内容类型的响应,而无需加载全部内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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