簡體   English   中英

添加更多參數時,RestSharp Azure Web API調用失敗

[英]RestSharp Azure Web API call fails when adding more parameters

我正在嘗試使用RestSharp調用Azure計算機視覺API,特別是[POST]批量讀取文件。 以下代碼中的一切正常:

private void MakeBatchReadRequest(string imageFilePath)
{
    try
    {
        RestClient client = new RestClient("https://southeastasia.api.cognitive.microsoft.com/");
        client.AddDefaultHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
        RestRequest request = new RestRequest("vision/v2.0/read/core/asyncBatchAnalyze", Method.POST);
        request.AddHeader("Content-Type", "application/octet-stream");

        byte[] byteData = GetImageAsByteArray(imageFilePath);
        request.AddParameter("application/octet-stream", byteData, ParameterType.RequestBody);

        RestResponse response = client.Execute(request);
        operationLocation = response.Headers.Where(x => x.Name == "Operation-Location").First.Value;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我沒有必須包含參數mode因為根據這里看到的API文檔,它是可選的,默認值是Printed ,這是我已經想要的。 但是,如果我在請求中添加參數mode (以防我改變主意並切換到其他方式),如下所示:

private void MakeBatchReadRequest(string imageFilePath)
{
    try
    {
        RestClient client = new RestClient("https://southeastasia.api.cognitive.microsoft.com/");
        client.AddDefaultHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
        RestRequest request = new RestRequest("vision/v2.0/read/core/asyncBatchAnalyze", Method.POST);
        request.AddHeader("Content-Type", "application/octet-stream");

        byte[] byteData = GetImageAsByteArray(imageFilePath);
        request.AddParameter("application/octet-stream", byteData, ParameterType.RequestBody);

        request.AddParameter("mode", "Printed") // This will cause the web API to return an error response.

        RestResponse response = client.Execute(request);
        operationLocation = response.Headers.Where(x => x.Name == "Operation-Location").First.Value;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

API返回響應狀態代碼415和狀態描述Unsupported Media Type 整個JSON響應如下:

{
    "error": {
        "code": "BadArgument",
        "message": "Unsupported media type."
    }
}

我不確定如何向請求添加簡單參數可能會觸發API的錯誤響應。 此外,我不確定為什么錯誤響應是Unsupported Media Type因為我使用的是支持的JPG圖像文件,並在請求中將其內容類型設置為application/octet-stream

任何幫助將不勝感激。

我升級到.Net Framework版本4.6.1和RestSharp版本106.6.9,它現在工作正常。 我想我別無選擇,只能升級我的應用程序的.Net Framework版本,這樣我就可以使用更新,更少錯誤的RestSharp版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM