Separate out QR web requests

Moved to Util
This commit is contained in:
Kaphotics 2016-08-30 20:31:14 -07:00
parent 037db77472
commit b6b931220d
3 changed files with 44 additions and 10 deletions

View file

@ -1,7 +1,5 @@
using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Web;
using System.Windows.Forms;
@ -57,10 +55,7 @@ namespace PKHeX
string webURL = "http://api.qrserver.com/v1/read-qr-code/?fileurl=" + HttpUtility.UrlEncode(address);
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webURL);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
var reader = new StreamReader(httpWebReponse.GetResponseStream());
string data = reader.ReadToEnd();
string data = Util.getStringFromURL(webURL);
if (data.Contains("could not find")) { Util.Alert("Reader could not find QR data in the image."); return null; }
if (data.Contains("filetype not supported")) { Util.Alert("Input URL is not valid. Double check that it is an image (jpg/png).", address); return null; }
// Quickly convert the json response to a data string
@ -82,10 +77,7 @@ namespace PKHeX
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webURL);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
if (stream != null) return Image.FromStream(stream);
return Util.getImageFromURL(webURL);
}
catch
{

View file

@ -304,6 +304,7 @@
<Compile Include="Util\DateUtil.cs" />
<Compile Include="Util\FormUtil.cs" />
<Compile Include="Util\ImageUtil.cs" />
<Compile Include="Util\NetUtil.cs" />
<Compile Include="Util\PathUtil.cs" />
<Compile Include="Util\RandUtil.cs" />
<Compile Include="Util\ReflectUtil.cs" />

41
PKHeX/Util/NetUtil.cs Normal file
View file

@ -0,0 +1,41 @@
using System;
using System.Drawing;
using System.IO;
using System.Net;
namespace PKHeX
{
public partial class Util
{
public static string getStringFromURL(string webURL)
{
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webURL);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
var reader = new StreamReader(httpWebReponse.GetResponseStream());
return reader.ReadToEnd();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
public static Image getImageFromURL(string webURL)
{
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(webURL);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
return stream != null ? Image.FromStream(stream) : null;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
}
}