调用 StackAPI 端点
Calling StackAPI endpoints
你好我正在写一个小任务来学习,查询Whosebug api
我想实现以下
● 当天提出的问题数量
● 所有问题的总浏览量
● 不同的(即没有重复的)逗号分隔的标签列表,用于该日期的所有问题,按字母顺序排列。
我已经完成了下面的 api 调用,但这不是我有 expected.First 我没有找到 api 终点来获取 no.of 问题的日期堆栈交换api。
非常感谢任何建议
static void Main()
{
Console.WriteLine("Making Whosebug API Call...");
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
{
client.BaseAddress = new Uri("https://api.stackexchange.com/docs");
// Int32 unixTimestamp = (Int32) (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1,0,0,0,0))).TotalSeconds;
var unixStamp = Helper.ConvertToDateTime("2017, 09, 23, 0, 0, 0");
var response = client.GetAsync("questions?fromdate="+ unixStamp + "&todate="+ unixStamp + "&site=Whosebug").Result;
response.EnsureSuccessStatusCode();
var s = response.Content.ReadAsStringAsync().Result;
var result = response.Content.ReadAsStringAsync().Result.Count();
Console.WriteLine("Total no.of questions asked: " + result);
}
Console.ReadLine();
}
首先你的基地址应该是https://api.stackexchange.com/2.2
。
要获取一天内创建的问题数,您必须修改默认文件管理器以包含 total
字段。
运行 以下请求:
将return:
{
"items": [],
"has_more": false,
"quota_max": 10000,
"quota_remaining": 9978,
"total": 3666
}
对于其他两个要求,您应该创建一个包含所需字段的新过滤器(如果您想处理 returned 问题,您可能希望在过滤器中包含更多字段以避免重复请求).创建见:filter/create and Custom Filters
得到结果后你必须解析 returned JSON 字符串(如果你想要 XML 被 returned 你必须解析 XML).您可以使用 Newtonsoft or the build in DataContractJsonSerializer (for more information see here).
这是一个使用内置序列化程序的示例:
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.IO;
[DataContract]
public class Question
{
// Question members...
}
[DataContract]
public class TotalQuestionsResult
{
[DataMember(Name = "items")]
public Question[] Items { get; set; }
[DataMember(Name = "has_more")]
public bool HasMore { get; set; }
[DataMember(Name = "quota_max")]
public int QuotaMax { get; set; }
[DataMember(Name = "quota_remaining")]
public int QuotaRemaining { get; set; }
[DataMember(Name = "total")]
public int Total { get; set; }
}
public async Task<int> GetTotalNumberOfQuestions()
{
// Get the data.
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
{
client.BaseAddress = new Uri("https://api.stackexchange.com/2.2");
var response = await client.GetAsync("questions?fromdate=1506211200&todate=1506297600&order=desc&sort=creation&site=Whosebug&filter=!)5IW-5QudQH7_nJ7d.eBuocQb(B)");
var responseStream = new MemoryStream(await response.Content.ReadAsByteArrayAsync());
var serializer = new DataContractJsonSerializer(typeof(TotalQuestionsResult));
var result = (TotalQuestionsResult)serializer.ReadObject(responseStream);
return result.Total;
}
}
你好我正在写一个小任务来学习,查询Whosebug api 我想实现以下 ● 当天提出的问题数量 ● 所有问题的总浏览量 ● 不同的(即没有重复的)逗号分隔的标签列表,用于该日期的所有问题,按字母顺序排列。
我已经完成了下面的 api 调用,但这不是我有 expected.First 我没有找到 api 终点来获取 no.of 问题的日期堆栈交换api。 非常感谢任何建议
static void Main()
{
Console.WriteLine("Making Whosebug API Call...");
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
{
client.BaseAddress = new Uri("https://api.stackexchange.com/docs");
// Int32 unixTimestamp = (Int32) (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1,0,0,0,0))).TotalSeconds;
var unixStamp = Helper.ConvertToDateTime("2017, 09, 23, 0, 0, 0");
var response = client.GetAsync("questions?fromdate="+ unixStamp + "&todate="+ unixStamp + "&site=Whosebug").Result;
response.EnsureSuccessStatusCode();
var s = response.Content.ReadAsStringAsync().Result;
var result = response.Content.ReadAsStringAsync().Result.Count();
Console.WriteLine("Total no.of questions asked: " + result);
}
Console.ReadLine();
}
首先你的基地址应该是https://api.stackexchange.com/2.2
。
要获取一天内创建的问题数,您必须修改默认文件管理器以包含 total
字段。
运行 以下请求:
将return:
{
"items": [],
"has_more": false,
"quota_max": 10000,
"quota_remaining": 9978,
"total": 3666
}
对于其他两个要求,您应该创建一个包含所需字段的新过滤器(如果您想处理 returned 问题,您可能希望在过滤器中包含更多字段以避免重复请求).创建见:filter/create and Custom Filters
得到结果后你必须解析 returned JSON 字符串(如果你想要 XML 被 returned 你必须解析 XML).您可以使用 Newtonsoft or the build in DataContractJsonSerializer (for more information see here).
这是一个使用内置序列化程序的示例:
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.IO;
[DataContract]
public class Question
{
// Question members...
}
[DataContract]
public class TotalQuestionsResult
{
[DataMember(Name = "items")]
public Question[] Items { get; set; }
[DataMember(Name = "has_more")]
public bool HasMore { get; set; }
[DataMember(Name = "quota_max")]
public int QuotaMax { get; set; }
[DataMember(Name = "quota_remaining")]
public int QuotaRemaining { get; set; }
[DataMember(Name = "total")]
public int Total { get; set; }
}
public async Task<int> GetTotalNumberOfQuestions()
{
// Get the data.
using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
{
client.BaseAddress = new Uri("https://api.stackexchange.com/2.2");
var response = await client.GetAsync("questions?fromdate=1506211200&todate=1506297600&order=desc&sort=creation&site=Whosebug&filter=!)5IW-5QudQH7_nJ7d.eBuocQb(B)");
var responseStream = new MemoryStream(await response.Content.ReadAsByteArrayAsync());
var serializer = new DataContractJsonSerializer(typeof(TotalQuestionsResult));
var result = (TotalQuestionsResult)serializer.ReadObject(responseStream);
return result.Total;
}
}