我想确认那些知道以下代码行是否只获取请求的响应类型而不加载完整内容的人,因为我必须在我的应用程序中的响应类型的某些条件,我不想要为那些应该忽略的资源浪费时间。
request = (HttpWebRequest)System.Net.HttpWebRequest.Create(url);
response = (HttpWebResponse)request.GetResponse();
var responseType = response.ContentType;
答案 0 :(得分:1)
将Method
property设置为"HEAD"
可能最容易 - 这样您就无法获得实际内容,只有标题。
另一方面,这意味着当您 想要内容时,您需要发出两个请求...
答案 1 :(得分:0)
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)
{
}