protected void LogResponse(HttpRequestInfo requestInfo, HttpTextResponse response) { var sb = new StringBuilder(); if (!response.IsSuccessful) { sb.AppendLine("Request Failed! " + requestInfo.Method + " " + requestInfo.Url); } else { sb.AppendLine("Request Succeeded! " + requestInfo.Method + " " + requestInfo.Url); } if (requestInfo.Headers != null) { foreach (var header in requestInfo.Headers) { sb.AppendLine(string.Format("{0}: {1}", header.Key, header.Value)); } } if (requestInfo.Body != null) { sb.AppendLine(); sb.AppendLine(requestInfo.Body); } sb.AppendLine(); sb.AppendLine("Response status " + (int)response.StatusCode); if (response.Headers != null) { foreach (var header in response.Headers) { sb.AppendLine(string.Format("{0}: {1}", header.Key, header.Value)); } } if (response.Text != null) { sb.AppendLine(); sb.AppendLine(response.Text); } if (!response.IsSuccessful) { Logger.LogError(sb.ToString()); } else { Logger.LogInfo(sb.ToString()); } }
protected void ThrowError(HttpTextResponse response) { var code = (ApiErrorCode)_apiErrorCode.GetValueOrDefault((int)ApiErrorCode.Unknown); // We could not get the code from the header, some other network error has occured if (code != ApiErrorCode.Unknown) { ErrorFactory.ThrowError(ToErrorCode(code), new HttpRequestException(response, _error, _errorDescription)); } else { ErrorFactory.ThrowError(ErrorCode.NetworkNotReachable, new HttpRequestException(response, _error, _errorDescription)); } }
public HttpResponseBaseData(HttpTextResponse response, ResponseType responseType) { try { _response = response; _responseJson = JsonObject.Parse(response.Text); _error = _responseJson.GetStringOrNull("error"); _errorDescription = _responseJson.GetStringOrNull("error_description"); } catch { } string errorCodeText; if (_response.Headers.TryGetValue("x-ca-err", out errorCodeText)) { int code; if (int.TryParse(errorCodeText, out code)) { _apiErrorCode = code; } } if (!_response.IsSuccessful) { ThrowError(response); } if ((responseType == ResponseType.Json || responseType == ResponseType.ScimJson) && _responseJson == null) { ErrorFactory.ThrowError(ErrorCode.ResponseSerializationFailedToParseResponse, new HttpRequestException(response, null, null)); } }
public HttpRequestException(HttpTextResponse responseData, string error, string errorDescription) { Response = responseData; Error = error; ErrorDescription = errorDescription; }
public override async Task <HttpTextResponse> RequestTextAsync(HttpRequestInfo requestInfo) { HttpTextResponse response = new HttpTextResponse(); HttpClient client; Logger.LogInfo("Requesting " + requestInfo.Method + " url " + requestInfo.Url); var handler = new HttpClientHandler(); handler.ClientCertificateOptions = ClientCertificateOption.Automatic; client = new HttpClient(handler); HttpRequestMessage httpRequest = new HttpRequestMessage(ToRequestMethod(requestInfo.Method), new Uri(requestInfo.Url)); if (requestInfo.Method == HttpMethod.GET || requestInfo.Method == HttpMethod.DELETE) { if (requestInfo.Headers != null) { foreach (var header in requestInfo.Headers) { httpRequest.Headers.Add(header.Key, header.Value); } } } else if (requestInfo.Method == HttpMethod.POST || requestInfo.Method == HttpMethod.PUT) { var content = new ByteArrayContent((requestInfo.Body ?? string.Empty).ToBytes()); if (requestInfo.Headers != null) { foreach (var header in requestInfo.Headers) { if (header.Key == HttpHeaders.ContentType) { content.Headers.ContentType = new MediaTypeHeaderValue(header.Value); } else { httpRequest.Headers.Add(header.Key, header.Value); } } } httpRequest.Content = content; } var httpResponse = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, _tokenSource.Token); response.StatusCode = (int)httpResponse.StatusCode; if (httpResponse.Headers != null) { response.Headers = new Dictionary <string, string>(); foreach (var header in httpResponse.Headers) { response.Headers[header.Key] = String.Join("; ", header.Value); } } response.Text = await httpResponse.Content.ReadAsStringAsync(); response.IsSuccessful = httpResponse.IsSuccessStatusCode; LogResponse(requestInfo, response); return(response); }
public override async Task <HttpTextResponse> RequestTextAsync(HttpRequestInfo requestInfo) { HttpTextResponse response = new HttpTextResponse(); Logger.LogInfo("Requesting Mutual SSL " + requestInfo.Method + " url " + requestInfo.Url); HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter(); filter.ClientCertificate = requestInfo.Certificate; HttpClient client = new HttpClient(filter); HttpRequestMessage httpRequest = new HttpRequestMessage(ToRequestMethod(requestInfo.Method), new Uri(requestInfo.Url)); if (requestInfo.Method == HttpMethod.GET || requestInfo.Method == HttpMethod.DELETE) { if (requestInfo.Headers != null) { foreach (var header in requestInfo.Headers) { httpRequest.Headers.TryAppendWithoutValidation(header.Key, header.Value); } } } else if (requestInfo.Method == HttpMethod.POST || requestInfo.Method == HttpMethod.PUT) { var content = new HttpStringContent(requestInfo.Body ?? string.Empty); if (requestInfo.Headers != null) { foreach (var header in requestInfo.Headers) { if (header.Key == HttpHeaders.ContentType) { content.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue(header.Value); } else { httpRequest.Headers.TryAppendWithoutValidation(header.Key, header.Value); } } } httpRequest.Content = content; } var task = client.SendRequestAsync(httpRequest); _activeTasks.Add(task); var httpResponse = await task; _activeTasks.Remove(task); response.StatusCode = (int)httpResponse.StatusCode; if (httpResponse.Headers != null) { response.Headers = new Dictionary <string, string>(); foreach (var header in httpResponse.Headers) { response.Headers[header.Key] = header.Value; } } response.Text = await httpResponse.Content.ReadAsStringAsync(); response.IsSuccessful = httpResponse.IsSuccessStatusCode; LogResponse(requestInfo, response); return(response); }
public HttpResponseBaseData(HttpTextResponse response) : this(response, ResponseType.Unknown) { }