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;
24 using System.Diagnostics.CodeAnalysis;
27 using System.Net.Http;
29 using System.Threading;
30 using System.Threading.Tasks;
32 namespace OpenTween.Connection
34 public static class Networking
36 public static TimeSpan DefaultTimeout { get; set; }
41 public static ProxyType ProxyType
43 get { return proxyType; }
49 public static IWebProxy Proxy
55 /// OpenTween 内で共通して使用する HttpClient インスタンス
57 public static HttpClient Http
59 get { return globalHttpClient; }
63 /// Webプロキシの設定が変更された場合に発生します
65 public static event EventHandler WebProxyChanged;
67 private static bool initialized = false;
68 private static HttpClient globalHttpClient;
69 private static ProxyType proxyType = ProxyType.IE;
70 private static IWebProxy proxy = null;
72 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
75 DefaultTimeout = TimeSpan.FromSeconds(20);
76 globalHttpClient = CreateHttpClient(new HttpClientHandler());
80 /// ネットワーク接続前に行う処理。起動時に一回だけ実行する必要があります。
82 public static void Initialize()
84 Networking.initialized = true;
86 ServicePointManager.Expect100Continue = false;
89 public static void SetWebProxy(ProxyType proxyType, string proxyAddress, int proxyPort,
90 string proxyUser, string proxyPassword)
98 case ProxyType.Specified:
99 proxy = new WebProxy(proxyAddress, proxyPort);
100 if (!string.IsNullOrEmpty(proxyUser) || !string.IsNullOrEmpty(proxyPassword))
101 proxy.Credentials = new NetworkCredential(proxyUser, proxyPassword);
105 proxy = WebRequest.GetSystemWebProxy();
109 Networking.proxyType = proxyType;
110 Networking.proxy = proxy;
112 NativeMethods.SetProxy(proxyType, proxyAddress, proxyPort, proxyUser, proxyPassword);
114 OnWebProxyChanged(EventArgs.Empty);
118 /// プロキシ等の設定を施した HttpClient インスタンスを生成します
121 /// 通常は Networking.Http を使用すべきです。
122 /// このメソッドを使用する場合は、WebProxyChanged イベントが発生する度に HttpClient を生成し直すように実装してください。
124 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
125 public static HttpClient CreateHttpClient(HttpClientHandler handler)
127 if (Networking.Proxy != null)
129 handler.UseProxy = true;
130 handler.Proxy = Networking.Proxy;
134 handler.UseProxy = false;
137 var client = new HttpClient(handler);
138 client.Timeout = Networking.DefaultTimeout;
139 client.DefaultRequestHeaders.Add("User-Agent", Networking.GetUserAgentString());
144 public static string GetUserAgentString(bool fakeMSIE = false)
147 return MyCommon.GetAssemblyName() + "/" + MyCommon.FileVersion + " (compatible; MSIE 10.0)";
149 return MyCommon.GetAssemblyName() + "/" + MyCommon.FileVersion;
153 /// Initialize() メソッドが事前に呼ばれているか確認します
155 internal static void CheckInitialized()
157 if (!Networking.initialized)
158 throw new InvalidOperationException("Sequence error.(not initialized)");
161 [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]
162 private static void OnWebProxyChanged(EventArgs e)
164 var newClient = Networking.CreateHttpClient(new HttpClientHandler());
165 var oldClient = Interlocked.Exchange(ref globalHttpClient, newClient);
168 if (WebProxyChanged != null)
169 WebProxyChanged(null, e);
173 public enum ProxyType