/// <summary> /// Sends the request asynchronous. /// </summary> /// <typeparam name="TRequest">The type of the request.</typeparam> /// <typeparam name="TResponse">The type of the response.</typeparam> /// <param name="httpMethod">The HTTP method.</param> /// <param name="requestUrl">The request URL.</param> /// <param name="requestBody">The request body.</param> /// <returns>The response.</returns> /// <exception cref="OxfordAPIException">The client exception.</exception> private async Task <TResponse> SendRequestAsync <TRequest, TResponse>(HttpMethod httpMethod, string requestUrl, TRequest requestBody) { var request = new HttpRequestMessage(httpMethod, ServiceHost); request.RequestUri = new Uri(requestUrl); if (requestBody != null) { if (requestBody is Stream) { request.Content = new StreamContent(requestBody as Stream); request.Content.Headers.ContentType = new MediaTypeHeaderValue(StreamContentTypeHeader); } else { request.Content = new StringContent(JsonConvert.SerializeObject(requestBody, s_settings), Encoding.UTF8, JsonContentTypeHeader); } } HttpResponseMessage response = await _httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { string responseContent = null; if (response.Content != null) { responseContent = await response.Content.ReadAsStringAsync(); } if (!string.IsNullOrWhiteSpace(responseContent)) { return(JsonConvert.DeserializeObject <TResponse>(responseContent, s_settings)); } return(default(TResponse)); } else { //Try throw an exception with details if (response.Content != null && response.Content.Headers.ContentType.MediaType.Contains(JsonContentTypeHeader)) { var errorObjectString = await response.Content.ReadAsStringAsync(); ClientError ex = JsonConvert.DeserializeObject <ClientError>(errorObjectString); if (ex?.Error != null) { throw new FaceAPIException(ex.Error.ErrorCode, ex.Error.Message, response.StatusCode); } ServiceError serviceEx = JsonConvert.DeserializeObject <ServiceError>(errorObjectString); if (serviceEx?.ErrorCode != null) { throw new FaceAPIException(serviceEx.ErrorCode, serviceEx.Message, response.StatusCode); } } //Fallback to a generic one throw new FaceAPIException("Unknown", "Unknown Error", response.StatusCode); } }
private async Task <string> SendRequestReturnStringAsync <TRequest, String>(HttpMethod httpMethod, string requestUrl, TRequest requestBody) { var request = new HttpRequestMessage(httpMethod, ServiceHost); request.RequestUri = new Uri(requestUrl); if (requestBody != null) { if (requestBody is Stream) { request.Content = new StreamContent(requestBody as Stream); request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); // byte[] m_Bytes = ReadToEnd(requestBody as Stream); //request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/Json"); } else { request.Content = new StringContent(JsonConvert.SerializeObject(requestBody, settings), Encoding.UTF8, JsonHeader); } } HttpResponseMessage response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { string responseContent = null; if (response.Content != null) { responseContent = await response.Content.ReadAsStringAsync(); } if (!string.IsNullOrWhiteSpace(responseContent)) { var a = responseContent; string xx = JsonConvert.SerializeObject(responseContent).ToString(); return(xx); } return(""); } else { if (response.Content != null && response.Content.Headers.ContentType.MediaType.Contains(JsonHeader)) { var errorObjectString = await response.Content.ReadAsStringAsync(); ClientError errorCollection = JsonConvert.DeserializeObject <ClientError>(errorObjectString); if (errorCollection != null) { throw new ClientException(errorCollection, response.StatusCode); } } response.EnsureSuccessStatusCode(); } return(""); }
private async Task <TResponse> SendRequestAsync <TRequest, TResponse>(HttpMethod httpMethod, string requestUrl, TRequest requestBody) { TResponse tResponse; HttpRequestMessage httpRequestMessage = new HttpRequestMessage(httpMethod, this.ServiceHostCn) { RequestUri = new Uri(requestUrl) }; if (requestBody != null) { if (!((object)requestBody is Stream)) { httpRequestMessage.Content = new StringContent(JsonConvert.SerializeObject(requestBody, FaceServiceClient.s_settings), Encoding.UTF8, "application/json"); } else { httpRequestMessage.Content = new StreamContent((object)requestBody as Stream); httpRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); } } HttpResponseMessage httpResponseMessage = await this._httpClient.SendAsync(httpRequestMessage); if (!httpResponseMessage.IsSuccessStatusCode) { if (httpResponseMessage.Content != null && httpResponseMessage.Content.Headers.ContentType.MediaType.Contains("application/json")) { string str = await httpResponseMessage.Content.ReadAsStringAsync(); ClientError clientError = JsonConvert.DeserializeObject <ClientError>(str); if (clientError.Error == null) { ServiceError serviceError = JsonConvert.DeserializeObject <ServiceError>(str); if (clientError == null) { throw new FaceAPIException("Unknown", "Unknown Error", httpResponseMessage.StatusCode); } throw new FaceAPIException(serviceError.ErrorCode, serviceError.Message, httpResponseMessage.StatusCode); } throw new FaceAPIException(clientError.Error.ErrorCode, clientError.Error.Message, httpResponseMessage.StatusCode); } httpResponseMessage.EnsureSuccessStatusCode(); tResponse = default(TResponse); } else { string str1 = null; if (httpResponseMessage.Content != null) { str1 = await httpResponseMessage.Content.ReadAsStringAsync(); } tResponse = (string.IsNullOrWhiteSpace(str1) ? default(TResponse) : JsonConvert.DeserializeObject <TResponse>(str1, FaceServiceClient.s_settings)); } return(tResponse); }
/// <summary> /// Process the exception happened on rest call. /// </summary> /// <param name="exception">Exception object.</param> private void HandleException(Exception exception) { WebException webException = exception as WebException; if (webException != null && webException.Response != null) { if (webException.Response.ContentType.ToLower().Contains("application/json")) { Stream stream = null; try { stream = webException.Response.GetResponseStream(); if (stream != null) { string errorObjectString; using (StreamReader reader = new StreamReader(stream)) { stream = null; errorObjectString = reader.ReadToEnd(); } ClientError errorCollection = JsonConvert.DeserializeObject <ClientError>(errorObjectString); if (errorCollection != null) { throw new ClientException { Error = errorCollection }; } } } finally { if (stream != null) { stream.Dispose(); } } } } throw exception; }
/// <summary> /// Initializes a new instance of the <see cref="ClientException"/> class. /// </summary> /// <param name="error">The error entity.</param> /// <param name="httpStatus">The http status.</param> public ClientException(ClientError error, HttpStatusCode httpStatus) { this.Error = error; this.HttpStatus = httpStatus; }