PowerToys/Wox.Infrastructure/Http/HttpRequest.cs

119 lines
3.9 KiB
C#
Raw Normal View History

2015-11-09 09:32:33 +08:00
using System.IO;
2014-12-21 22:03:03 +08:00
using System.Net;
using System.Text;
using Wox.Plugin;
namespace Wox.Infrastructure.Http
{
public class HttpRequest
{
public static string Get(string url, IHttpProxy proxy, string encoding = "UTF-8")
2014-12-21 22:03:03 +08:00
{
return Get(url, encoding, proxy);
2014-12-21 22:03:03 +08:00
}
2015-02-01 22:46:56 +08:00
public static WebProxy GetWebProxy(IHttpProxy proxy)
2014-12-21 22:03:03 +08:00
{
if (proxy != null && proxy.Enabled && !string.IsNullOrEmpty(proxy.Server))
{
if (string.IsNullOrEmpty(proxy.UserName) || string.IsNullOrEmpty(proxy.Password))
{
2015-02-01 22:46:56 +08:00
return new WebProxy(proxy.Server, proxy.Port);
2014-12-21 22:03:03 +08:00
}
2015-02-01 22:46:56 +08:00
return new WebProxy(proxy.Server, proxy.Port)
2014-12-21 22:03:03 +08:00
{
2015-02-01 22:46:56 +08:00
Credentials = new NetworkCredential(proxy.UserName, proxy.Password)
};
2014-12-21 22:03:03 +08:00
}
2015-02-01 22:46:56 +08:00
return null;
}
private static string Get(string url, string encoding, IHttpProxy proxy)
{
if (string.IsNullOrEmpty(url)) return string.Empty;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.Timeout = 10 * 1000;
request.Proxy = GetWebProxy(proxy);
2014-12-21 22:03:03 +08:00
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response != null)
{
Stream stream = response.GetResponseStream();
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding(encoding)))
{
return reader.ReadToEnd();
}
}
}
}
2015-11-09 09:32:33 +08:00
catch (System.Exception e)
2014-12-21 22:03:03 +08:00
{
2015-01-11 21:52:30 +08:00
Logger.Log.Error(e);
return string.Empty;
}
return string.Empty;
}
public static string Post(string url, string jsonData, IHttpProxy proxy)
{
if (string.IsNullOrEmpty(url)) return string.Empty;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "text/json";
request.Timeout = 10 * 1000;
if (proxy != null && proxy.Enabled && !string.IsNullOrEmpty(proxy.Server))
{
if (string.IsNullOrEmpty(proxy.UserName) || string.IsNullOrEmpty(proxy.Password))
{
request.Proxy = new WebProxy(proxy.Server, proxy.Port);
}
else
{
request.Proxy = new WebProxy(proxy.Server, proxy.Port)
{
Credentials = new NetworkCredential(proxy.UserName, proxy.Password)
};
}
}
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(jsonData);
streamWriter.Flush();
streamWriter.Close();
}
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response != null)
{
Stream stream = response.GetResponseStream();
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
{
return reader.ReadToEnd();
}
}
}
}
2015-11-09 09:32:33 +08:00
catch (System.Exception e)
2015-01-11 21:52:30 +08:00
{
Logger.Log.Error(e);
2014-12-21 22:03:03 +08:00
return string.Empty;
}
return string.Empty;
}
}
}