【发布时间】:2021-05-04 12:17:56
【问题描述】:
我正在编写一个从 WooCommerce 商店检索产品的代码。 API 只返回 100 个产品,而总数达到 147 个。API 返回两个页面,但我似乎无法遍历这些页面。
这是我目前的代码:
protected void btnEnviar_Click(object sender, EventArgs e)
{
try
{
string apiUrl = "https://thestore.com/wp-json/wc/v3/products?per_page=100";
string apiUsr = "usertoken";
string apiPwd = "passwordtoken";
string urlRequest1 = apiUrl;
HttpWebRequest requestWeb = (HttpWebRequest)WebRequest.Create(urlRequest1);
requestWeb.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
requestWeb.ContentType = "application/json";
requestWeb.Accept = "application/json";
requestWeb.Method = WebRequestMethods.Http.Get;
requestWeb.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(apiUsr + ":" +apiPwd));
requestWeb.UseDefaultCredentials = true;
requestWeb.Proxy.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse responseHttpWeb = (HttpWebResponse)requestWeb.GetResponse();
Stream stream = responseHttpWeb.GetResponseStream();
//JsonConvert.DeserializeObject<List<RetrieveMultipleResponse>>(JsonStr);
using (StreamReader reader = new StreamReader(stream))
{
Stream webResponse = responseHttpWeb.GetResponseStream();
string lector = reader.ReadToEnd();
var mQuery = JsonConvert.DeserializeObject<List<Root>>(lector);
int contarObjetos = mQuery.Count();
contarItems.Text = contarObjetos.ToString();
if (contarObjetos == 0)
{
enviado.Text = "No existen registros";
}
else
{
foreach (var item in mQuery)
{
enviado.Text += item.id + ": " + item.name + " - " + item.status + "<br/><hr/>";
foreach(var elemento in item.categories)
{
enviado2.Text += elemento.id + ": " + elemento.name + "<br/><hr/>";
}
}
}
}
}
catch (Exception ex)
{
enviado.Text = ex.ToString();
}
}
我已尝试更改 Uri 参数“per_page”,但任何超过 100 的值都会出错。
我可以修改Uri添加一个“page”参数,这样会带上page 1,page 2等:
string apiUrl = "https://antonelly.com.co/wp-json/wc/v3/products?per_page=100&page=2";
但是如何从 api 的标头中获取页数并动态地将其分配给循环?
这是我在 Postman 上看到的标题:
所以我想如果我可以访问该标头并将其分配给一个对象并在每个循环中增加,我将实现我正在寻找的。p>
我在https://woocommerce.github.io/woocommerce-rest-api-docs/?python#list-all-products 浏览了 WooCommerce 的 API 文档,但没有关于如何执行此操作的信息,并且在 JSON 的响应中没有返回返回页面数量的对象。
任何帮助对我来说都是巨大的!
【问题讨论】:
-
你试过
responseHttpWeb.Headers吗?例如:string numPages = responseHttpWeb.Headers["X-WP-TotalPages"]; int numPages2; if (int.TryParse(numPages, out numPages2)) { // Success! }如果给你页数,我可以写几行代码来提取其他页。 -
是的!它带回了页数(在这种情况下为 2 页)谢谢。但是现在,我该如何循环浏览这些页面?
标签: c# asp.net json api woocommerce