mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-09 20:57:22 +02:00
let it crash for http get
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
@@ -47,32 +48,27 @@ namespace Wox.Infrastructure.Http
|
|||||||
/// <exception cref="WebException">Can't get response from http get </exception>
|
/// <exception cref="WebException">Can't get response from http get </exception>
|
||||||
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
|
public static async Task<string> Get([NotNull] string url, string encoding = "UTF-8")
|
||||||
{
|
{
|
||||||
|
var request = WebRequest.CreateHttp(url);
|
||||||
HttpWebRequest request = WebRequest.CreateHttp(url);
|
|
||||||
request.Method = "GET";
|
request.Method = "GET";
|
||||||
request.Timeout = 10 * 1000;
|
request.Timeout = 1000;
|
||||||
request.Proxy = WebProxy();
|
request.Proxy = WebProxy();
|
||||||
request.UserAgent = UserAgent;
|
request.UserAgent = UserAgent;
|
||||||
var response = await request.GetResponseAsync() as HttpWebResponse;
|
var response = await request.GetResponseAsync() as HttpWebResponse;
|
||||||
if (response != null)
|
response = response.NonNull();
|
||||||
|
var stream = response.GetResponseStream().NonNull();
|
||||||
|
|
||||||
|
using (var reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
|
||||||
{
|
{
|
||||||
var stream = response.GetResponseStream();
|
var content = await reader.ReadToEndAsync();
|
||||||
if (stream != null)
|
if (response.StatusCode == HttpStatusCode.OK)
|
||||||
{
|
{
|
||||||
using (var reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
|
return content;
|
||||||
{
|
|
||||||
return await reader.ReadToEndAsync();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return string.Empty;
|
throw new HttpRequestException($"Error code <{response.StatusCode}> with content <{content}> returned from <{url}>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user