Flow.Launcher/Flow.Launcher.Infrastructure/Http/Http.cs

109 lines
3.7 KiB
C#
Raw Normal View History

using System.IO;
2014-12-21 14:03:03 +00:00
using System.Net;
2017-03-07 18:57:47 +00:00
using System.Net.Http;
2014-12-21 14:03:03 +00:00
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using System;
2014-12-21 14:03:03 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Infrastructure.Http
2014-12-21 14:03:03 +00:00
{
public static class Http
2014-12-21 14:03:03 +00:00
{
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
private static HttpClient client;
private static SocketsHttpHandler socketsHttpHandler = new SocketsHttpHandler()
{
UseProxy = true,
Proxy = WebProxy
};
static Http()
{
// need to be added so it would work on a win10 machine
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
2020-03-02 01:22:26 +00:00
| SecurityProtocolType.Tls12;
client = new HttpClient(socketsHttpHandler, false);
client.DefaultRequestHeaders.Add("User-Agent", UserAgent);
}
private static HttpProxy proxy;
public static HttpProxy Proxy
{
private get
{
return proxy;
}
set
{
proxy = value;
UpdateProxy();
}
}
public static WebProxy WebProxy { get; private set; } = new WebProxy();
/// <summary>
/// Update the Address of the Proxy to modify the client Proxy
/// </summary>
public static void UpdateProxy()
// TODO: need test with a proxy
2014-12-21 14:03:03 +00:00
{
2016-06-19 15:18:43 +00:00
if (Proxy != null && Proxy.Enabled && !string.IsNullOrEmpty(Proxy.Server))
2014-12-21 14:03:03 +00:00
{
2016-06-19 15:18:43 +00:00
if (string.IsNullOrEmpty(Proxy.UserName) || string.IsNullOrEmpty(Proxy.Password))
2014-12-21 14:03:03 +00:00
{
WebProxy.Address = new Uri($"http://{Proxy.Server}:{Proxy.Port}");
WebProxy.Credentials = null;
2014-12-21 14:03:03 +00:00
}
else
2014-12-21 14:03:03 +00:00
{
WebProxy.Address = new Uri($"http://{Proxy.Server}:{Proxy.Port}");
WebProxy.Credentials = new NetworkCredential(Proxy.UserName, Proxy.Password);
}
}
else
{
WebProxy.Address = new WebProxy().Address;
WebProxy.Credentials = null;
2014-12-21 14:03:03 +00:00
}
2015-02-01 14:46:56 +00:00
}
public async static Task Download([NotNull] string url, [NotNull] string filePath)
{
using var response = await client.GetAsync(url);
if (response.StatusCode == HttpStatusCode.OK)
{
using var fileStream = new FileStream(filePath, FileMode.CreateNew);
await response.Content.CopyToAsync(fileStream);
}
else
{
throw new WebException($"Error code <{response.StatusCode}> returned from <{url}>");
}
}
2016-06-19 15:18:43 +00:00
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
2015-02-01 14:46:56 +00:00
{
2017-03-26 23:08:56 +00:00
Log.Debug($"|Http.Get|Url <{url}>");
var response = await client.GetAsync(url);
using var stream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream, Encoding.GetEncoding(encoding));
var content = await reader.ReadToEndAsync();
if (response.StatusCode == HttpStatusCode.OK)
2015-01-11 13:52:30 +00:00
{
return content;
}
else
{
throw new HttpRequestException($"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
2015-01-11 13:52:30 +00:00
}
2014-12-21 14:03:03 +00:00
}
}
}