PowerToys/Wox.Infrastructure/Http/Http.cs

74 lines
2.6 KiB
C#
Raw Normal View History

using System.IO;
2014-12-21 22:03:03 +08:00
using System.Net;
2017-03-08 02:57:47 +08:00
using System.Net.Http;
2014-12-21 22:03:03 +08:00
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
2017-03-27 07:08:56 +08:00
using Wox.Infrastructure.Logger;
2016-06-19 23:18:43 +08:00
using Wox.Infrastructure.UserSettings;
2014-12-21 22:03:03 +08:00
namespace Wox.Infrastructure.Http
{
public static class Http
2014-12-21 22:03:03 +08:00
{
private const string UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
2016-06-19 23:18:43 +08:00
public static HttpProxy Proxy { private get; set; }
public static IWebProxy WebProxy()
2014-12-21 22:03:03 +08:00
{
2016-06-19 23:18:43 +08:00
if (Proxy != null && Proxy.Enabled && !string.IsNullOrEmpty(Proxy.Server))
2014-12-21 22:03:03 +08:00
{
2016-06-19 23:18:43 +08:00
if (string.IsNullOrEmpty(Proxy.UserName) || string.IsNullOrEmpty(Proxy.Password))
2014-12-21 22:03:03 +08:00
{
2016-06-19 23:18:43 +08:00
var webProxy = new WebProxy(Proxy.Server, Proxy.Port);
return webProxy;
2014-12-21 22:03:03 +08:00
}
else
2014-12-21 22:03:03 +08:00
{
2016-06-19 23:18:43 +08:00
var webProxy = new WebProxy(Proxy.Server, Proxy.Port)
{
2016-06-19 23:18:43 +08:00
Credentials = new NetworkCredential(Proxy.UserName, Proxy.Password)
};
return webProxy;
}
}
else
{
2016-05-14 06:28:17 +08:00
return WebRequest.GetSystemWebProxy();
2014-12-21 22:03:03 +08:00
}
2015-02-01 22:46:56 +08:00
}
2016-06-19 23:18:43 +08:00
public static void Download([NotNull] string url, [NotNull] string filePath)
{
2016-06-19 23:18:43 +08:00
var client = new WebClient { Proxy = WebProxy() };
client.Headers.Add("user-agent", UserAgent);
client.DownloadFile(url, filePath);
}
2016-06-19 23:18:43 +08:00
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
2015-02-01 22:46:56 +08:00
{
2017-03-27 07:08:56 +08:00
Log.Debug($"|Http.Get|Url <{url}>");
2017-03-08 02:57:47 +08:00
var request = WebRequest.CreateHttp(url);
2015-02-01 22:46:56 +08:00
request.Method = "GET";
2017-03-08 02:57:47 +08:00
request.Timeout = 1000;
2016-06-19 23:18:43 +08:00
request.Proxy = WebProxy();
request.UserAgent = UserAgent;
var response = await request.GetResponseAsync() as HttpWebResponse;
2017-03-08 02:57:47 +08:00
response = response.NonNull();
var stream = response.GetResponseStream().NonNull();
using (var reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
2015-01-11 21:52:30 +08:00
{
2017-03-08 02:57:47 +08:00
var content = await reader.ReadToEndAsync();
if (response.StatusCode == HttpStatusCode.OK)
2015-01-11 21:52:30 +08:00
{
2017-03-08 02:57:47 +08:00
return content;
2015-01-11 21:52:30 +08:00
}
else
2015-01-11 21:52:30 +08:00
{
2017-03-08 02:57:47 +08:00
throw new HttpRequestException($"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
2015-01-11 21:52:30 +08:00
}
}
2014-12-21 22:03:03 +08:00
}
}
}