C# 错误404使用MS认知服务图像分析演示代码
使用MS Cognitive services示例应用程序中的代码,尝试在imageFilePath设置为本地JPEG文件的情况下运行以下代码时出现错误404:C# 错误404使用MS认知服务图像分析演示代码,c#,microsoft-cognitive,C#,Microsoft Cognitive,使用MS Cognitive services示例应用程序中的代码,尝试在imageFilePath设置为本地JPEG文件的情况下运行以下代码时出现错误404: static async void MakeAnalysisRequest(string imageFilePath) { HttpClient client = new HttpClient(); // Request headers. client.DefaultReques
static async void MakeAnalysisRequest(string imageFilePath)
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters. A third optional parameter is "details".
string requestParameters = "visualFeatures=Description&language=en";
// Assemble the URI for the REST API Call.
string uri = uriBase + "?" + requestParameters;
HttpResponseMessage response;
// Request body. Posts a locally stored JPEG image.
byte[] byteData = GetImageAsByteArray(imageFilePath);
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json" and "multipart/form-data".
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
response = await client.PostAsync(uri, content);
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n");
Console.WriteLine(JsonPrettyPrint(contentString));
}
subscriptionKey是my Azure订阅(位于澳大利亚东部)提供的两个密钥中的密钥1,uribase是
const string uriBase = "https://australiaeast.api.cognitive.microsoft.com/vision/v1.0";
KEY1和uriBase的组合可以在
https://australiaeast.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa/console
当目标文件是URL而不是本地文件时。正确分析图像
为什么我在尝试从C#发帖时会出现404错误?我使用的是内容类型应用程序/八位字节流吗
GetImageAsByteArray的代码为:
static byte[] GetImageAsByteArray(string imageFilePath)
{
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
您缺少请求URI的最后一部分。应该是:
stringuri=uriBase+“/analyze?”+requestParameters;
您缺少请求URI的最后一部分。应该是:
stringuri=uriBase+“/analyze?”+requestParameters;
内容类型以及发送订阅密钥的方式都应该良好。GetImageAsByteArray
看起来像什么?这些文档有以下内容-byte[]byteData=Encoding.UTF8.GetBytes(“{body}”)代码>内容类型以及发送订阅密钥的方式应良好。GetImageAsByteArray
看起来像什么?这些文档有以下内容-byte[]byteData=Encoding.UTF8.GetBytes(“{body}”)代码>就是这样!此外,我还必须使用Azure subscriptionEither的键2而不是键1。该键应该可以工作。如果重新生成密钥,前端服务器可能需要一些时间才能识别它,这可能就是为什么只有您的密钥2起作用的原因。另一个问题是MakeAnalysisRequest中存在两个wait命令。控件在第一个例程之后返回到调用例程,该例程只发布请求,而不是等待接收响应。使用计时器解决了这个问题,但不是很优雅。您可能希望MakeAnalysisRequest
返回任务,而不是void
。这将允许调用方等待
。尝试从asp.net网页调用MakeAnalysisRequest时,我的计时器解决方案不起作用。当执行等待功能时,控制停止。你能推荐一些从web应用程序使用图像分析web服务的代码示例吗?我能找到的所有例子都来自Windows应用程序。就是这样!此外,我还必须使用Azure subscriptionEither的键2而不是键1。该键应该可以工作。如果重新生成密钥,前端服务器可能需要一些时间才能识别它,这可能就是为什么只有您的密钥2起作用的原因。另一个问题是MakeAnalysisRequest中存在两个wait命令。控件在第一个例程之后返回到调用例程,该例程只发布请求,而不是等待接收响应。使用计时器解决了这个问题,但不是很优雅。您可能希望MakeAnalysisRequest
返回任务,而不是void
。这将允许调用方等待
。尝试从asp.net网页调用MakeAnalysisRequest时,我的计时器解决方案不起作用。当执行等待功能时,控制停止。你能推荐一些从web应用程序使用图像分析web服务的代码示例吗?我能找到的所有例子都来自Windows应用程序。