mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
* Moved Logger/Log.cs from Wox.Infrastructure to Wox.Plugin
- Installed Logger dependency in Wox.Plugin: NLog.Extensions.Logging
- Moved file Log.cs from Wox.Infrastructure/Logger/ to Wox.Plugin/Logger
- Moved file Constant.cs from Wox.Infrastructure to Wox.Plugin: This file was moved since Log.cs depends on this class
- Copied Wox.Infrastructure.Helper.NonNull to Wox.Plugin.Constant since Constant.cs depends on this method
- Replaced all "using Wox.Infrastructure.Logger" to "using Wox.Plugin.Logger" in all files as needed
- Replaced Wox.Infrastructure.Constant to Wox.Plugin.Constant in all files as needed
* Removed Nlog.Extensions.Logging from Wox.Infrastructure
* Added logging and suppressed general exceptions (CA1031: Do not catch general exception types)
* Resolved fxcop errors introduced by newly added Log.cs
- CA1307: Specify StringComparison for clarity
- CA2000: Dispose objects before losing scope
- CA1062: Validate arguments of public methods
* Replaced Wox.Infrastructure.Logger with Wox.Plugin.Logger
91 lines
3.2 KiB
C#
91 lines
3.2 KiB
C#
// Copyright (c) Microsoft Corporation
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using JetBrains.Annotations;
|
|
using Wox.Infrastructure.UserSettings;
|
|
using Wox.Plugin.Logger;
|
|
|
|
namespace Wox.Infrastructure.Http
|
|
{
|
|
public static class Http
|
|
{
|
|
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
|
|
|
|
static Http()
|
|
{
|
|
// need to be added so it would work on a win10 machine
|
|
ServicePointManager.Expect100Continue = true;
|
|
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls
|
|
| SecurityProtocolType.Tls11
|
|
| SecurityProtocolType.Tls12;
|
|
}
|
|
|
|
public static HttpProxy Proxy { private get; set; }
|
|
|
|
public static IWebProxy WebProxy()
|
|
{
|
|
if (Proxy != null && Proxy.Enabled && !string.IsNullOrEmpty(Proxy.Server))
|
|
{
|
|
if (string.IsNullOrEmpty(Proxy.UserName) || string.IsNullOrEmpty(Proxy.Password))
|
|
{
|
|
var webProxy = new WebProxy(Proxy.Server, Proxy.Port);
|
|
return webProxy;
|
|
}
|
|
else
|
|
{
|
|
var webProxy = new WebProxy(Proxy.Server, Proxy.Port)
|
|
{
|
|
Credentials = new NetworkCredential(Proxy.UserName, Proxy.Password),
|
|
};
|
|
return webProxy;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return WebRequest.GetSystemWebProxy();
|
|
}
|
|
}
|
|
|
|
public static void Download([NotNull] string url, [NotNull] string filePath)
|
|
{
|
|
var client = new WebClient { Proxy = WebProxy() };
|
|
client.Headers.Add("user-agent", UserAgent);
|
|
client.DownloadFile(url, filePath);
|
|
}
|
|
|
|
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
|
|
{
|
|
Log.Debug($"Url <{url}>", MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
var request = WebRequest.CreateHttp(url);
|
|
request.Method = "GET";
|
|
request.Timeout = 1000;
|
|
request.Proxy = WebProxy();
|
|
request.UserAgent = UserAgent;
|
|
var response = await request.GetResponseAsync() as HttpWebResponse;
|
|
response = response.NonNull();
|
|
var stream = response.GetResponseStream().NonNull();
|
|
|
|
using (var reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
|
|
{
|
|
var content = await reader.ReadToEndAsync();
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
return content;
|
|
}
|
|
else
|
|
{
|
|
throw new HttpRequestException($"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|