2017-03-12 6 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 

をVisual Studioを使用している場合、これは応答のためのC#タイプを含むあなたのための世俗的な詳細の多くを処理するよう、私はNuGet packageをお勧めします:ここで動作するコードです。

+0

神様は、「あなたのJSONは形式が違っています。あなたのフィールドと非スカラーフィールドは引用しなければなりません」と私の頭を壁に叩いた後、このサイトは以前、乞食の信念を打ち負かしています。 1つのジュニア開発者から別のシニア開発者に感謝します。 – Wazzie

関連する問題