private async Task <HttpResponseMessage> UrlRequest(string request, HttpMethod httpMethod, IEnumerable <KeyValuePair <string, string> > data = null, string referer = null) { if (string.IsNullOrEmpty(request) || (httpMethod == null)) { ArchiLogger.LogNullError(nameof(request) + " || " + nameof(httpMethod)); return(null); } HttpResponseMessage responseMessage; using (HttpRequestMessage requestMessage = new HttpRequestMessage(httpMethod, request)) { if (data != null) { try { requestMessage.Content = new FormUrlEncodedContent(data); } catch (UriFormatException e) { ArchiLogger.LogGenericException(e); return(null); } } if (!string.IsNullOrEmpty(referer)) { requestMessage.Headers.Referrer = new Uri(referer); } try { responseMessage = await HttpClient.SendAsync(requestMessage).ConfigureAwait(false); } catch (Exception e) { // This exception is really common, don't bother with it unless debug mode is enabled if (Debugging.IsUserDebugging) { ArchiLogger.LogGenericDebugException(e); } return(null); } } if (responseMessage == null) { return(null); } if (responseMessage.IsSuccessStatusCode) { return(responseMessage); } if (Debugging.IsUserDebugging) { ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, request)); ArchiLogger.LogGenericDebug(string.Format(Strings.StatusCode, responseMessage.StatusCode)); ArchiLogger.LogGenericDebug(string.Format(Strings.Content, await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false))); } responseMessage.Dispose(); return(null); }
private async Task <HttpResponseMessage> UrlRequest(Uri requestUri, HttpMethod httpMethod, ICollection <KeyValuePair <string, string> > data = null, string referer = null, byte maxRedirections = MaxTries) { if ((requestUri == null) || (httpMethod == null)) { ArchiLogger.LogNullError(nameof(requestUri) + " || " + nameof(httpMethod)); return(null); } HttpResponseMessage responseMessage; using (HttpRequestMessage requestMessage = new HttpRequestMessage(httpMethod, requestUri)) { if (data != null) { try { requestMessage.Content = new FormUrlEncodedContent(data); } catch (UriFormatException e) { ArchiLogger.LogGenericException(e); return(null); } } if (!string.IsNullOrEmpty(referer)) { requestMessage.Headers.Referrer = new Uri(referer); } try { responseMessage = await HttpClient.SendAsync(requestMessage).ConfigureAwait(false); } catch (Exception e) { // This exception is really common, don't bother with it unless debug mode is enabled if (Debugging.IsUserDebugging) { ArchiLogger.LogGenericDebugException(e); } return(null); } } if (responseMessage == null) { return(null); } if (responseMessage.IsSuccessStatusCode) { return(responseMessage); } Uri redirectUri; using (responseMessage) { ushort status = (ushort)responseMessage.StatusCode; if ((status >= 300) && (status <= 399) && (maxRedirections > 0)) { redirectUri = responseMessage.Headers.Location; if (!redirectUri.IsAbsoluteUri) { redirectUri = new Uri(requestUri.GetLeftPart(UriPartial.Authority) + redirectUri); } } else { if (!Debugging.IsDebugBuild) { return(null); } ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, requestUri)); ArchiLogger.LogGenericDebug(string.Format(Strings.StatusCode, responseMessage.StatusCode)); ArchiLogger.LogGenericDebug(string.Format(Strings.Content, await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false))); return(null); } } return(await UrlRequest(redirectUri, httpMethod, data, referer, --maxRedirections).ConfigureAwait(false)); }
private async Task <HttpResponseMessage> UrlRequest(string request, HttpMethod httpMethod, IEnumerable <KeyValuePair <string, string> > data = null, string referer = null) { if (string.IsNullOrEmpty(request) || (httpMethod == null)) { ArchiLogger.LogNullError(nameof(request) + " || " + nameof(httpMethod)); return(null); } if (request.StartsWith("https://", StringComparison.Ordinal) && Program.GlobalConfig.ForceHttp) { return(null); } HttpResponseMessage responseMessage; using (HttpRequestMessage requestMessage = new HttpRequestMessage(httpMethod, request)) { if (data != null) { try { requestMessage.Content = new FormUrlEncodedContent(data); } catch (UriFormatException e) { ArchiLogger.LogGenericException(e); return(null); } } if (!string.IsNullOrEmpty(referer)) { requestMessage.Headers.Referrer = new Uri(referer); } try { responseMessage = await HttpClient.SendAsync(requestMessage).ConfigureAwait(false); } catch (Exception e) { // This exception is really common, don't bother with it unless debug mode is enabled if (Debugging.IsDebugBuild || Program.GlobalConfig.Debug) { ArchiLogger.LogGenericDebugException(e); } return(null); } } if (responseMessage == null) { return(null); } if (responseMessage.IsSuccessStatusCode) { return(responseMessage); } if (Debugging.IsDebugBuild || Program.GlobalConfig.Debug) { ArchiLogger.LogGenericDebug("Request: " + request + " failed!"); ArchiLogger.LogGenericDebug("Status code: " + responseMessage.StatusCode); ArchiLogger.LogGenericDebug("Content: " + Environment.NewLine + await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false)); } responseMessage.Dispose(); return(null); }