2017-03-12 2 views
0

이 잘못된 요청 오류를 해결하기 위해 노력하고 있습니다. 요청 전화를 할 수 있고 Azure는 총 호출을 올바르게보고하고 총 오류도보고합니다.Microsoft Face Detect API 코드 예 잘못된 요청

이 코드 예제는 작동하지 않습니다. 그러나 온라인 콘솔을 통해이를 보내면 모두 괜찮습니다.

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 
+0

코드를 다시 포맷 할 때 인공 줄 바꿈 (예 :'data' 및'uri')이있는 여러 코드 줄을 보았습니다. 여기에 붙여 넣을 때 의도적 이었나요? 그렇지 않은 경우 문제가 발생할 수 있습니다. –

+0

분명히 의도적 인 문제는 아니며 Microsoft인지 서비스에서이 사례를 사용하고있었습니다. – Wazzie

답변

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 

하는 당신은 비주얼 스튜디오를 사용하는 경우는,이 ​​같은 NuGet package는 응답 C 번호 유형을 포함하여, 당신을위한 일상적인 세부 사항의 대부분을 처리합니다 추천 할 것입니다.

+0

신이 말하길 "JSON의 형식이 잘못되었습니다. 필드와 비 스칼라 필드는 벽에 머리를 대고 4 주 후에 인용해야합니다." 이 사이트는 전에 거지 신념을 이겼습니다. 한 주니어 개발자에서 다른 고위 개발자에게 감사드립니다. – Wazzie