【发布时间】:2017-05-12 21:19:28
【问题描述】:
Bing 几个月前发布了他们的 Search API 的新版本 (2)。 MS Cognitive Services 提供了使用其新搜索 API 2 的示例。
https://dev.cognitive.microsoft.com/docs/services/56b43f0ccf5ff8098cef3808/operations/571fab09dbe2d933e891028f
但是,工作示例带有 XML 响应,并且只有部分 C# 示例进行调用,但不解码结果。
有人可以解释或继续示例代码,以便将返回的对象解析为实际图像或图像 URL 吗?在下面的代码中,“content”变量的类型是 ByteArrayContent,但是从这个数组中获取信息需要什么? “response”变量的类型是HttpResponseMessage,但是如何从中提取图像或图像URL?
这将允许应用程序选择并显示一个或多个返回的图像。
非常感谢,蒂姆
这是 C# 代码示例:
using System;
using System.Net.Http.Headers;
using System.Text;
using System.Net.Http;
using System.Web;
namespace CSHttpClientSample
{
static class Program
{
static void Main()
{
MakeRequest();
Console.WriteLine("Hit ENTER to exit...");
Console.ReadLine();
}
static async void MakeRequest()
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key",
"{subscription key}");
// Request parameters
queryString["q"] = "cats";
var uri =
"https://api.cognitive.microsoft.com/bing/v5.0/images/search?"
+ queryString;
HttpResponseMessage response;
// Request body
byte[] byteData = Encoding.UTF8.GetBytes("{body}");
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue(
"< your content type, i.e. application/json >");
response = await client.PostAsync(uri, content);
}
}
}
}
【问题讨论】:
标签: c# arrays parsing httpclient bing-api