gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:54:57 27 4
gpt4 key购买 nike

我一直在尝试解决这个错误的请求错误。我能够发出请求调用,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

最佳答案

您的 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# 类型。

关于azure - Microsoft Face Detect API 代码示例错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42753159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com