Microsoft Face Detect API代码示例错误请求

时间:2017-03-12 21:06:19

标签: azure microsoft-cognitive

我一直在尝试解决这个错误的请求错误。我可以进行请求调用,Azure可以正确报告总呼叫数,并报告总错误。

我无法使用此代码示例;但是,如果我通过他们的在线控制台发送这一切都很好:

  static async void MakeRequest()
    {       
        string key1 = "YourKey"; // azure the one should work 
        string data = "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);

        Console.Beep();

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

        //string statusURL = HttpContext.Current.Request.Url.Host;
        //console.WriteLine("Your Status URL address is :" + statusURL);

        HttpResponseMessage response;

        // Request body
        //  byte[] byteData = Encoding.UTF8.GetBytes("{url:    https://pbs.twimg.com/profile_images/476054279438868480/vvv5YG0Q.jpeg}");

        byte[] byteData = Encoding.UTF8.
       GetBytes("{"+ "url"+":"+"https://pbs.twimg.com/profile_images/476054279438868480/vvv5YG0Q.jpeg" + "}");

        using (var content = new ByteArrayContent(byteData))
        {
            content.Headers.ContentType = 
            new  MediaTypeHeaderValue("application/json");
            response = await client.PostAsync(uri, content);
        }

        HttpRequestMessage request = 
        new HttpRequestMessage(HttpMethod.Post, uri);

        request.Content = new StringContent("{body}",
                                            Encoding.UTF8,
                                            "application/json");
        //CONTENT-TYPE header

        await client.SendAsync(request)
              .ContinueWith(responseTask =>
              {
                  Console.WriteLine("Response: {0}", responseTask.Result);
                  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 

1 个答案:

答案 0 :(得分:2)

您的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,我会推荐NuGet package,因为这会为你处理大部分平凡的细节,包括回复的C#类型。