我正在调用Face API和该API的图像,并在Json中获取结果。我想在循环中再调用一次此api,然后将所有图像的所有结果保存在数组中的循环中。不幸的是我做不到。
foreach (var allwords in ViewBag.sentence)
{
string imgpath = Server.MapPath("~/Content/Images/") + allwords + ".png";
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add(
"Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters. A third optional parameter is "details".
string requestParameters = "&returnFaceAttributes=smile," +
"emotion";
// Assemble the URI for the REST API Call.
string uri = uriBase + "?" + requestParameters;
HttpResponseMessage response;
// Request body. Posts a locally stored JPEG image.
byte[] byteData = GetImageAsByteArray(imgpath);
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json"
// and "multipart/form-data".
content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
response = await client.PostAsync(uri, content);
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// var data = JsonConvert.DeserializeObject(contentString);
//array to store every image result
string results = contentString;
// return Json(result, JsonRequestBehavior.AllowGet);
}
我只得到一个图像结果,这是Web API调用的最后一个图像。我正在循环调用所有图像,但无法保存在最后一个图像之前调用的图像的结果。