using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
+using System.Threading;
using System.Threading.Tasks;
using System.Web;
static ShortUrl()
{
- _instance = new Lazy<ShortUrl>(() =>
- {
- var handler = new HttpClientHandler
- {
- AllowAutoRedirect = false,
- };
-
- var http = MyCommon.CreateHttpClient(handler);
- http.Timeout = new TimeSpan(0, 0, seconds: 5);
+ _instance = new Lazy<ShortUrl>(() => new ShortUrl(), true);
+ }
- return new ShortUrl(http);
- }, true);
+ internal ShortUrl()
+ : this(CreateDefaultHttpClient())
+ {
+ HttpConnection.WebProxyChanged += (o, e) =>
+ {
+ var newClient = CreateDefaultHttpClient();
+ var oldClient = Interlocked.Exchange(ref this.http, newClient);
+ oldClient.Dispose();
+ };
}
internal ShortUrl(HttpClient http)
return response.Headers.Location;
}
}
+
+ private static HttpClient CreateDefaultHttpClient()
+ {
+ var handler = new HttpClientHandler
+ {
+ AllowAutoRedirect = false,
+ };
+
+ var http = HttpConnection.CreateHttpClient(handler);
+ http.Timeout = TimeSpan.FromSeconds(5);
+
+ return http;
+ }
}
}