public static IPAddress GetPublicIP() { IPAddress result; try { IOHttpRequestSettings settings = new IOHttpRequestSettings { OnResponseReceived = r => ((HttpWebResponse)r).StatusCode == HttpStatusCode.OK }; WebRequest request = UriHelper.MakeHttpWebRequest(STR_PUBLIC_IP, settings); string content = request.ReadToEnd(settings); if (string.IsNullOrEmpty(content)) { return(null); } Match match = __extractIP.Match(content); result = match.Success ? IPAddress.Parse(match.Groups["ip"].Value) : null; } catch { result = null; } return(result); }
public static HttpClientEx Create(Uri baseUri, IOHttpRequestSettings settings) { settings ??= new IOHttpRequestSettings(); HttpClientEx client = new HttpClientEx(new ChunkedTransferEncodingHandler(new HttpClientHandler().Configure(settings))); client.Configure(baseUri, settings); return(client); }
public static T Configure <T>([NotNull] this T thisValue, IOHttpRequestSettings settings = null) where T : HttpClientHandler { settings ??= new IOHttpRequestSettings(); thisValue.AllowAutoRedirect = settings.AllowAutoRedirect; thisValue.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; thisValue.UseDefaultCredentials = settings.Credentials == null; thisValue.Credentials = settings.Credentials ?? CredentialCache.DefaultNetworkCredentials; thisValue.ClientCertificateOptions = ClientCertificateOption.Automatic; thisValue.Proxy = settings.Proxy; thisValue.UseProxy = settings.Proxy != null; return(thisValue); }
public static IOHttpRequestSettings GetRequestSettings([NotNull] this HttpClient thisValue) { HttpRequestHeaders headers = thisValue.DefaultRequestHeaders; IOHttpRequestSettings settings = new IOHttpRequestSettings { Timeout = thisValue.Timeout.TotalIntMilliseconds(), Accept = thisValue.DefaultRequestHeaders.Accept.ToList() }; try { string encoding = headers.AcceptEncoding.FirstOrDefault()?.Value; settings.Encoding = encoding == null ? Encoding.UTF8 : Encoding.GetEncoding(encoding); } catch { settings.Encoding = Encoding.UTF8; } return(settings); }
public static T Configure <T>([NotNull] this T thisValue, Uri baseAddress = null, IOHttpRequestSettings settings = null) where T : HttpClient { if (baseAddress != null) { thisValue.BaseAddress = baseAddress; } HttpRequestHeaders headers = thisValue.DefaultRequestHeaders; headers.CacheControl = new CacheControlHeaderValue { NoCache = true }; if (settings == null) { return(thisValue); } if (settings.Timeout > 0) { thisValue.Timeout = TimeSpan.FromMilliseconds(settings.Timeout); } if (settings.Accept.Count > 0) { headers.Accept.Clear(); foreach (MediaTypeWithQualityHeaderValue headerValue in settings.Accept) { headers.Accept.Add(headerValue); } } if (settings.Encoding != null) { headers.AcceptEncoding.Clear(); headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(settings.Encoding.WebName)); } return(thisValue); }
/// <inheritdoc /> protected HttpClientEx(HttpMessageHandler handler, bool disposeHandler, Uri baseUrl, IOHttpRequestSettings settings) : base(handler, disposeHandler) { this.Configure(baseUrl, settings); }
/// <inheritdoc /> protected HttpClientEx(Uri baseUrl, IOHttpRequestSettings settings) : this(new HttpClientHandler(), true, baseUrl, settings) { }
public static HttpClientEx Create([NotNull] string baseUri, IOHttpRequestSettings settings) { return(Create(UriHelper.ToUri(baseUri), settings)); }
public static HttpClientEx Create(IOHttpRequestSettings settings) { return(Create((Uri)null, settings)); }
/// <inheritdoc /> protected HttpClientEx([NotNull] string baseUrl, IOHttpRequestSettings settings) : this(new HttpClientHandler(), true, UriHelper.ToUri(baseUrl), settings) { }