Azure Microsoft Face检测API代码示例错误请求

Azure Microsoft Face检测API代码示例错误请求,azure,microsoft-cognitive,Azure,Microsoft Cognitive,我一直在试图解决这个错误的请求。我能够进行请求呼叫,Azure报告正确的呼叫总数,并报告全部错误 我无法让这个代码示例工作;但是,如果我通过他们的在线控制台发送此信息,则一切正常: 静态异步void MakeRequest() { string key1=“YourKey”;//一个应该可以工作 字符串数据=”https://pbs.twimg.com/profile_images/476054279438868480/vvv5YG0Q.jpeg"; var client=新的Ht

我一直在试图解决这个错误的请求。我能够进行请求呼叫,Azure报告正确的呼叫总数,并报告全部错误

我无法让这个代码示例工作;但是,如果我通过他们的在线控制台发送此信息,则一切正常:

静态异步void MakeRequest()
{       
string key1=“YourKey”;//一个应该可以工作
字符串数据=”https://pbs.twimg.com/profile_images/476054279438868480/vvv5YG0Q.jpeg";
var client=新的HttpClient();
var queryString=HttpUtility.ParseQueryString(string.Empty);
//请求参数
queryString[“returnFaceId”]=“true”;
//请求头
client.DefaultRequestHeaders.Add(“Ocp Apim订阅密钥”,key1);
Console.Beep();
var uri=”https://westus.api.cognitive.microsoft.com/face/v1.0/detect?“+质询;
//字符串statusURL=HttpContext.Current.Request.Url.Host;
//console.WriteLine(“您的状态URL地址为:“+statusURL”);
HttpResponseMessage响应;
//请求主体
//byte[]byteData=Encoding.UTF8.GetBytes(“{url:https://pbs.twimg.com/profile_images/476054279438868480/vvv5YG0Q.jpeg}");
字节[]byteData=Encoding.UTF8。
GetBytes(“{”+”url“+”:“+”https://pbs.twimg.com/profile_images/476054279438868480/vvv5YG0Q.jpeg" + "}");
使用(var内容=新的ByteArrayContent(byteData))
{
content.Headers.ContentType=
新的MediaTypeHeaderValue(“应用程序/json”);
response=wait client.PostAsync(uri、内容);
}
HttpRequestMessage请求=
新的HttpRequestMessage(HttpMethod.Post,uri);
request.Content=newstringcontent({body}),
Encoding.UTF8,
“应用程序/json”);
//内容类型标题
等待client.SendAsync(请求)
.ContinueWith(responseTask=>
{
WriteLine(“响应:{0}”,responseTask.Result);
Console.WriteLine(“-----------------------------------------”;
Console.ForegroundColor=ConsoleColor.Blue;
Console.WriteLine(“MS返回后结束”);
Console.WriteLine(“按回车键退出…”);
Console.ReadKey();
});
}//请求结束

您的JSON格式不正确。必须引用字段和非标量字段。您还有一些不必要的代码。下面是有效的代码:

static async void MakeRequest()
{       
    string key1 = "YourKey"; // azure the one should work 
    string imageUri = "https://pbs.twimg.com/profile_images/476054279438868480/vvv5YG0Q.jpeg";

    var client = new HttpClient();
    var queryString = HttpUtility.ParseQueryString(string.Empty);

    // Request parameters
    queryString["returnFaceId"] = "true";

    // Request headers
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key1);

    var uri = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect?"  + queryString;

    string body = "{\"url\":\"" + imageUri + "\"}";

    using (var content = new StringContent(body, Encoding.UTF8, "application/json"))
    {
        await client.PostAsync(uri, content)
            .ContinueWith(async responseTask =>
            {
                var responseBody = await responseTask.Result.Content.ReadAsStringAsync();
                Console.WriteLine("Response: {0}", responseBody);
                Console.WriteLine("-----------------------------------");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("End of Post return from MS");
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadKey();
            });
    }
}// end of Make request

如果您使用的是Visual Studio,我建议使用,因为这将为您处理许多常见的细节,包括用于响应的C类型。

您的JSON格式不正确。必须引用字段和非标量字段。您还有一些不必要的代码。下面是有效的代码:

static async void MakeRequest()
{       
    string key1 = "YourKey"; // azure the one should work 
    string imageUri = "https://pbs.twimg.com/profile_images/476054279438868480/vvv5YG0Q.jpeg";

    var client = new HttpClient();
    var queryString = HttpUtility.ParseQueryString(string.Empty);

    // Request parameters
    queryString["returnFaceId"] = "true";

    // Request headers
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key1);

    var uri = "https://westus.api.cognitive.microsoft.com/face/v1.0/detect?"  + queryString;

    string body = "{\"url\":\"" + imageUri + "\"}";

    using (var content = new StringContent(body, Encoding.UTF8, "application/json"))
    {
        await client.PostAsync(uri, content)
            .ContinueWith(async responseTask =>
            {
                var responseBody = await responseTask.Result.Content.ReadAsStringAsync();
                Console.WriteLine("Response: {0}", responseBody);
                Console.WriteLine("-----------------------------------");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("End of Post return from MS");
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadKey();
            });
    }
}// end of Make request

如果您使用的是Visual Studio,我建议使用,因为这将为您处理许多常见的细节,包括用于响应的C类型。

在重新格式化代码时,我看到(并更正)几个带有人工换行符的代码行(例如用于
数据
uri
)。你粘贴在这里是故意的吗?如果不是,那肯定会导致问题。这绝对不是故意的。在这种情况下,我使用的是Microsoft认知服务的这个示例。在重新格式化代码时,我看到(并更正)几个带有人工换行符的代码行(例如,
数据
uri
)。你粘贴在这里是故意的吗?如果不是,那肯定会引起问题。这绝对不是故意的。在这种情况下,我使用的是Microsoft认知服务的这个例子。天哪,这一切归结为:“你的JSON格式不正确。你的字段和非标量字段必须被引用”,在我头撞墙4周后,无论你是谁,我都非常感谢你,为什么我以前没有来过这个网站。从一个初级开发人员到另一个高级开发人员,谢谢你。天哪,这一切归结为:“你的JSON格式不正确。你的字段和非标量字段必须被引用”。在我头撞墙4周后,无论你是谁,我都非常感谢你,为什么我没有在这个网站上出现,这比乞丐的信念更让人难以置信。从一个初级开发人员到另一个高级开发人员,谢谢。