如何处理来自 http 的 json
How to handle json from http
我使用 WebClient class 导航到网站。它 returns 我有一个下载对话框,用于下载 json 文件中的输出。 json 文件中的内容是否可以在不下载的情况下阅读?我打开 chrome 并粘贴相同的 url 和 chrome 向我展示了浏览器本身的输出。
只需向 link 发出请求,您将在字符串中获得 JSON:
public static Response MakeRequest(string requestUrl){
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
Response jsonResponse
= objResponse as Response;
return jsonResponse;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
这取决于内容类型和浏览器,在某些情况下,内容可能会压缩。 Whosebug API 就是这样一个例子。
在这些情况下,您需要设置 request.AutomaticDecompression
。下面的代码可能会让您对理解有一个清晰的认识并从那里继续。
public string CallRequest(Uri url)
{
var request = WebRequest.Create(url) as HttpWebRequest;
var httpResponse = "";
if (request != null)
{
request.UserAgent = "Whosebug"; // just example.
request.Accept = "gzip,deflate";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (var response = request.GetResponse() as HttpWebResponse)
{
using (var responseStream = response.GetResponseStream())
{
var reader = new StreamReader(responseStream);
httpResponse = reader.ReadToEnd();
}
}
}
return httpResponse;
}
我使用 WebClient class 导航到网站。它 returns 我有一个下载对话框,用于下载 json 文件中的输出。 json 文件中的内容是否可以在不下载的情况下阅读?我打开 chrome 并粘贴相同的 url 和 chrome 向我展示了浏览器本身的输出。
只需向 link 发出请求,您将在字符串中获得 JSON:
public static Response MakeRequest(string requestUrl){
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
Response jsonResponse
= objResponse as Response;
return jsonResponse;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
这取决于内容类型和浏览器,在某些情况下,内容可能会压缩。 Whosebug API 就是这样一个例子。
在这些情况下,您需要设置 request.AutomaticDecompression
。下面的代码可能会让您对理解有一个清晰的认识并从那里继续。
public string CallRequest(Uri url)
{
var request = WebRequest.Create(url) as HttpWebRequest;
var httpResponse = "";
if (request != null)
{
request.UserAgent = "Whosebug"; // just example.
request.Accept = "gzip,deflate";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (var response = request.GetResponse() as HttpWebResponse)
{
using (var responseStream = response.GetResponseStream())
{
var reader = new StreamReader(responseStream);
httpResponse = reader.ReadToEnd();
}
}
}
return httpResponse;
}