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;
|
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
|
|
public static class NetUtil
|
2016-08-31 03:31:14 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public static string? GetStringFromURL(Uri url)
|
2016-08-31 03:31:14 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
try
|
2016-08-31 03:31:14 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var stream = GetStreamFromURL(url);
|
|
|
|
|
if (stream == null)
|
2016-08-31 03:31:14 +00:00
|
|
|
|
return null;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
using var reader = new StreamReader(stream);
|
|
|
|
|
return reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
// No internet?
|
|
|
|
|
catch (Exception e)
|
2019-01-26 00:51:58 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
Debug.WriteLine(e.Message);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Stream? GetStreamFromURL(Uri url)
|
|
|
|
|
{
|
|
|
|
|
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
|
2019-01-26 00:51:58 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +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";
|
2019-01-26 00:51:58 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var httpWebResponse = httpWebRequest.GetResponse();
|
|
|
|
|
return httpWebResponse.GetResponseStream();
|
2016-08-31 03:31:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|