/// <summary> /// Submits an image for OCR analysis. The analysis is async, so a second call needs to be made to retrieve the results. /// </summary> /// <param name="imageAsBytes">the image to process, as a byte array</param> /// <returns>url where the result of analysis can be found. or null.</returns> /// <remarks> /// Method requires a Response 202 from initial image response /// The service has accepted the request and will start processing later. /// It will return Accepted immediately and include an "Operation-Location" header. /// Client side should further query the read operation status using the URL specified in this header. /// The operation ID will expire in 48 hours. /// </remarks> private async Task <string> MakeOcrAnalysisRequestToAzure(byte[] imageAsBytes) { using (var client = new HttpClient()) { try { client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ConnectionDetails.SubscriptionKey); var url = ConnectionDetails.GetOcrUrl(); HttpResponseMessage response; using (ByteArrayContent content = new ByteArrayContent(imageAsBytes)) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response = await client.PostAsync(url, content); } // Operation-Location is the URI where the results can be found. There will only be 1 value here, but .NET doesn't let us do this quickly. if (response.Headers.TryGetValues("Operation-Location", out var headerValues)) { return(headerValues.FirstOrDefault()); } } catch (HttpRequestException ex) { Console.WriteLine(ex); throw; } } return(null); }
/// <summary> /// Submits an image for OCR analysis. The analysis is async, so a second call needs to be made to retrieve the results. /// </summary> /// <param name="imageByteArray">the image to process, as a byte array</param> /// <returns>url where the result of analysis can be found. or null.</returns> /// <remarks> /// Method requires a Response 202 from initial image response /// The service has accepted the request and will start processing later. /// It will return Accepted immediately and include an "Operation-Location" header. /// Client side should further query the read operation status using the URL specified in this header. /// The operation ID will expire in 48 hours. /// </remarks> private string MakeOcrAnalysisRequestToAzure(byte[] imageByteArray) { string callbackUrl = null; HttpWebResponse response = null; try { var request = WebRequest.CreateHttp(new Uri(ConnectionDetails.GetOcrUrl())); request.ContentType = "application/octet-stream"; request.Headers.Add("Ocp-Apim-Subscription-Key", ConnectionDetails.SubscriptionKey); request.Method = "POST"; request.ContentLength = imageByteArray.Length; var requestStream = request.GetRequestStream(); requestStream.Write(imageByteArray, 0, imageByteArray.Length); requestStream.Close(); response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { throw new HttpRequestException("did not return success"); } callbackUrl = response.Headers["Operation-Location"]; } catch (HttpRequestException ex) { Console.WriteLine(ex); throw; } finally { response?.Close(); } return(callbackUrl); }