[英]C# Get content type of a response without loading entire content
我想确保那些知道以下代码行是否只获取请求的响应类型而不加载完整内容的人,因为我必须在我的应用程序中的响应类型的某些条件,我不想浪费时间对于那些应该被忽略的资源。
request = (HttpWebRequest)System.Net.HttpWebRequest.Create(url);
response = (HttpWebResponse)request.GetResponse();
var responseType = response.ContentType;
将Method
属性设置为"HEAD"
可能最简单 - 这样您就不会获得实际内容,只会获得标题。
在另一方面,这并不意味着你需要做出两个请求当你想要的内容?
Please use below code to get the data in response string
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "application/json";
try
{
WebResponse webResponse = request.GetResponse();
using (Stream webStream = webResponse.GetResponseStream())
{
if (webStream != null)
{
using (StreamReader responseReader = new StreamReader(webStream))
{
string response = responseReader.ReadToEnd();
JObject objjson = JObject.Parse(response);
var obj= (from p in objjson["YourobjectList"].Children()
select p).ToList();
}
}
}
}
catch (Exception ex)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.