namespace OpenTween
{
- public sealed class MyCommon
+ public static class MyCommon
{
private static readonly object LockObj = new object();
public static bool _endingFlag; //終了フラグ
case DialogResult.No:
return false;
case DialogResult.Cancel:
- return IsTerminatePermission;
default:
- throw new Exception("");
+ return IsTerminatePermission;
}
}
}
//RTByMe
}
- public static string GetUserAgentString(bool fakeMSIE = false)
- {
- if (fakeMSIE)
- return GetAssemblyName() + "/" + FileVersion + " (compatible; MSIE 10.0)";
- else
- return GetAssemblyName() + "/" + FileVersion;
- }
-
public static TwitterApiStatus TwitterApiInfo = new TwitterApiStatus();
public static bool IsAnimatedGif(string filename)
}
/// <summary>
- /// OpenTween 内で共通して使う設定を施した HttpClient インスタンスを作成します
+ /// 指定された IDictionary を元にクエリ文字列を生成します
/// </summary>
- public static HttpClient CreateHttpClient()
+ /// <param name="param">生成するクエリの key-value コレクション</param>
+ public static string BuildQueryString(IDictionary<string, string> param)
{
- return CreateHttpClient(new HttpClientHandler());
+ if (param == null || param.Count == 0)
+ return string.Empty;
+
+ var query = param
+ .Where(x => x.Value != null)
+ .Select(x => EscapeQueryString(x.Key) + '=' + EscapeQueryString(x.Value));
+
+ return string.Join("&", query);
}
+ // .NET 4.5+: Reserved characters のうち、Uriクラスによってエスケープ強制解除されてしまうものも最初から Unreserved として扱う
+ private static readonly HashSet<char> UnreservedChars =
+ new HashSet<char>("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~!'()*:");
+
/// <summary>
- /// OpenTween 内で共通して使う設定を施した HttpClient インスタンスを作成します
+ /// 2バイト文字も考慮したクエリ用エンコード
/// </summary>
- public static HttpClient CreateHttpClient(HttpClientHandler handler)
+ /// <param name="stringToEncode">エンコードする文字列</param>
+ /// <returns>エンコード結果文字列</returns>
+ public static string EscapeQueryString(string stringToEncode)
{
- switch (HttpConnection.proxyKind)
+ var sb = new StringBuilder(stringToEncode.Length * 2);
+
+ foreach (var b in Encoding.UTF8.GetBytes(stringToEncode))
{
- case HttpConnection.ProxyType.None:
- handler.UseProxy = false;
- break;
- case HttpConnection.ProxyType.Specified:
- handler.UseProxy = true;
- handler.Proxy = HttpConnection.proxy;
- break;
- case HttpConnection.ProxyType.IE:
- default:
- handler.UseProxy = true;
- handler.Proxy = WebRequest.GetSystemWebProxy();
- break;
+ if (UnreservedChars.Contains((char)b))
+ sb.Append((char)b);
+ else
+ sb.AppendFormat("%{0:X2}", b);
}
- var client = new HttpClient(handler);
- client.DefaultRequestHeaders.Add("User-Agent", MyCommon.GetUserAgentString());
-
- return client;
+ return sb.ToString();
}
}
}