2016-08-31 03:31:14 +00:00
|
|
|
|
using System;
|
2017-07-02 02:43:51 +00:00
|
|
|
|
using System.Diagnostics;
|
2016-08-31 03:31:14 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
2019-09-29 16:47:06 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-08-31 03:31:14 +00:00
|
|
|
|
{
|
2017-02-04 16:18:48 +00:00
|
|
|
|
public static class NetUtil
|
2016-08-31 03:31:14 +00:00
|
|
|
|
{
|
2020-06-17 02:46:22 +00:00
|
|
|
|
public static string? GetStringFromURL(string url)
|
2016-08-31 03:31:14 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2020-06-17 02:46:22 +00:00
|
|
|
|
var stream = GetStreamFromURL(url);
|
2021-03-14 23:16:55 +00:00
|
|
|
|
if (stream == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2019-10-08 01:40:09 +00:00
|
|
|
|
using var reader = new StreamReader(stream);
|
|
|
|
|
return reader.ReadToEnd();
|
2016-08-31 03:31:14 +00:00
|
|
|
|
}
|
2020-09-19 05:11:13 +00:00
|
|
|
|
#pragma warning disable CA1031 // Do not catch general exception types
|
|
|
|
|
// No internet?
|
2016-08-31 03:31:14 +00:00
|
|
|
|
catch (Exception e)
|
2020-09-19 05:11:13 +00:00
|
|
|
|
#pragma warning restore CA1031 // Do not catch general exception types
|
2016-08-31 03:31:14 +00:00
|
|
|
|
{
|
2017-07-02 02:43:51 +00:00
|
|
|
|
Debug.WriteLine(e.Message);
|
2016-08-31 03:31:14 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2021-03-14 23:16:55 +00:00
|
|
|
|
private static Stream? GetStreamFromURL(string url)
|
2019-01-26 00:51:58 +00:00
|
|
|
|
{
|
2020-06-17 02:46:22 +00:00
|
|
|
|
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
|
2019-01-26 00:51:58 +00:00
|
|
|
|
|
|
|
|
|
// The GitHub API will fail if no user agent is provided
|
|
|
|
|
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
|
|
|
|
|
|
|
|
|
|
var httpWebResponse = httpWebRequest.GetResponse();
|
|
|
|
|
return httpWebResponse.GetResponseStream();
|
|
|
|
|
}
|
2016-08-31 03:31:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|