1 // OpenTween - Client of Twitter
2 // Copyright (c) 2014 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
23 using System.Collections.Generic;
26 using System.Net.Http;
28 using System.Threading;
29 using System.Threading.Tasks;
31 namespace OpenTween.Connection
33 public static class Networking
35 public static TimeSpan DefaultTimeout { get; set; }
40 public static ProxyType ProxyType
42 get { return proxyType; }
48 public static IWebProxy Proxy
54 /// OpenTween 内で共通して使用する HttpClient インスタンス
56 public static HttpClient Http
58 get { return globalHttpClient; }
62 /// Webプロキシの設定が変更された場合に発生します
64 public static event EventHandler WebProxyChanged;
66 private static bool initialized = false;
67 private static HttpClient globalHttpClient;
68 private static ProxyType proxyType = ProxyType.IE;
69 private static IWebProxy proxy = null;
73 DefaultTimeout = TimeSpan.FromSeconds(20);
74 globalHttpClient = CreateHttpClient(new HttpClientHandler());
78 /// ネットワーク接続前に行う処理。起動時に一回だけ実行する必要があります。
80 public static void Initialize()
82 Networking.initialized = true;
84 ServicePointManager.Expect100Continue = false;
87 public static void SetWebProxy(ProxyType proxyType, string proxyAddress, int proxyPort,
88 string proxyUser, string proxyPassword)
96 case ProxyType.Specified:
97 proxy = new WebProxy(proxyAddress, proxyPort);
98 if (!string.IsNullOrEmpty(proxyUser) || !string.IsNullOrEmpty(proxyPassword))
99 proxy.Credentials = new NetworkCredential(proxyUser, proxyPassword);
103 proxy = WebRequest.GetSystemWebProxy();
107 Networking.proxyType = proxyType;
108 Networking.proxy = proxy;
110 Win32Api.SetProxy(proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword);
112 OnWebProxyChanged(EventArgs.Empty);
116 /// プロキシ等の設定を施した HttpClient インスタンスを生成します
119 /// 通常は Networking.Http を使用すべきです。
120 /// このメソッドを使用する場合は、WebProxyChanged イベントが発生する度に HttpClient を生成し直すように実装してください。
122 public static HttpClient CreateHttpClient(HttpClientHandler handler)
124 if (Networking.Proxy != null)
126 handler.UseProxy = true;
127 handler.Proxy = Networking.Proxy;
131 handler.UseProxy = false;
134 var client = new HttpClient(handler);
135 client.Timeout = Networking.DefaultTimeout;
136 client.DefaultRequestHeaders.Add("User-Agent", Networking.GetUserAgentString());
141 public static string GetUserAgentString(bool fakeMSIE = false)
144 return MyCommon.GetAssemblyName() + "/" + MyCommon.FileVersion + " (compatible; MSIE 10.0)";
146 return MyCommon.GetAssemblyName() + "/" + MyCommon.FileVersion;
150 /// Initialize() メソッドが事前に呼ばれているか確認します
152 internal static void CheckInitialized()
154 if (!Networking.initialized)
155 throw new InvalidOperationException("Sequence error.(not initialized)");
158 private static void OnWebProxyChanged(EventArgs e)
160 var newClient = Networking.CreateHttpClient(new HttpClientHandler());
161 var oldClient = Interlocked.Exchange(ref globalHttpClient, newClient);
164 if (WebProxyChanged != null)
165 WebProxyChanged(null, e);
169 public enum ProxyType