OSDN Git Service

HttpConnection.GlobalHttpClientを追加, MyCommon.CreateHttpClient()をHttpConnectionクラスに移動
[opentween/open-tween.git] / OpenTween / ShortUrl.cs
index bf89cd9..4250c47 100644 (file)
@@ -31,6 +31,7 @@ using System.Linq;
 using System.Net.Http;
 using System.Text;
 using System.Text.RegularExpressions;
+using System.Threading;
 using System.Threading.Tasks;
 using System.Web;
 
@@ -122,18 +123,18 @@ namespace OpenTween
 
         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)
@@ -488,5 +489,18 @@ namespace OpenTween
                 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;
+        }
     }
 }