use unified http method for plugin installation + add more exceptions

#573
#610
This commit is contained in:
bao-qian 2016-05-10 20:26:47 +01:00
parent 8325083402
commit 6359826fd9
7 changed files with 111 additions and 108 deletions

View file

@ -1,34 +0,0 @@
using System;
using System.Net;
namespace Wox.Plugin.PluginManagement
{
public class HttpRequest
{
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
public static HttpWebResponse CreateGetHttpResponse(string url,IHttpProxy proxy)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("url");
}
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
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);
request.Proxy.Credentials = new NetworkCredential(proxy.UserName, proxy.Password);
}
}
request.Method = "GET";
request.UserAgent = DefaultUserAgent;
return request.GetResponse() as HttpWebResponse;
}
}
}

View file

@ -8,6 +8,8 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Newtonsoft.Json;
using Wox.Infrastructure.Http;
using Wox.Infrastructure.Logger;
namespace Wox.Plugin.PluginManagement
{
@ -108,68 +110,71 @@ namespace Wox.Plugin.PluginManagement
List<Result> results = new List<Result>();
string pluginName = query.SecondSearch;
if (string.IsNullOrEmpty(pluginName)) return results;
HttpWebResponse response = HttpRequest.CreateGetHttpResponse(pluginSearchUrl + pluginName, context.Proxy);
Stream s = response.GetResponseStream();
if (s != null)
string json;
try
{
StreamReader reader = new StreamReader(s, Encoding.UTF8);
string json = reader.ReadToEnd();
List<WoxPluginResult> searchedPlugins = null;
try
{
searchedPlugins = JsonConvert.DeserializeObject<List<WoxPluginResult>>(json);
}
catch
{
context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty);
return results;
}
var task = Http.Get(pluginSearchUrl + pluginName, context.Proxy);
task.RunSynchronously();
json = task.Result;
}
catch (WebException e)
{
Log.Warn("Can't connect to Wox plugin website, check your conenction");
Log.Error(e);
return new List<Result>();
}
List<WoxPluginResult> searchedPlugins;
try
{
searchedPlugins = JsonConvert.DeserializeObject<List<WoxPluginResult>>(json);
}
catch(JsonSerializationException e)
{
context.API.ShowMsg("Coundn't parse api search results", "Please update your Wox!", string.Empty);
Log.Error(e);
return results;
}
foreach (WoxPluginResult r in searchedPlugins)
foreach (WoxPluginResult r in searchedPlugins)
{
WoxPluginResult r1 = r;
results.Add(new Result
{
WoxPluginResult r1 = r;
results.Add(new Result
Title = r.name,
SubTitle = r.description,
IcoPath = "Images\\plugin.png",
Action = c =>
{
Title = r.name,
SubTitle = r.description,
IcoPath = "Images\\plugin.png",
Action = e =>
MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
"Install plugin", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
MessageBoxResult result = MessageBox.Show("Are your sure to install " + r.name + " plugin",
"Install plugin", MessageBoxButton.YesNo);
string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
if (result == MessageBoxResult.Yes)
context.API.StartLoadingBar();
string pluginUrl = APIBASE + "/media/" + r1.plugin_file;
try
{
string folder = Path.Combine(Path.GetTempPath(), "WoxPluginDownload");
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
string filePath = Path.Combine(folder, Guid.NewGuid().ToString() + ".wox");
context.API.StartLoadingBar();
Task.Run(() =>
{
using (WebClient Client = new WebClient())
{
try
{
string pluginUrl = APIBASE + "/media/" + r1.plugin_file;
Client.DownloadFile(pluginUrl, filePath);
context.API.InstallPlugin(filePath);
}
catch (Exception exception)
{
MessageBox.Show("download plugin " + r.name + "failed. " + exception.Message);
}
finally
{
context.API.StopLoadingBar();
}
}
});
Http.Download(pluginUrl, filePath, context.Proxy);
}
return false;
catch (WebException e)
{
var info = "download plugin " + r.name + "failed.";
MessageBox.Show(info);
Log.Warn(info);
Log.Error(e);
return false;
}
context.API.InstallPlugin(filePath);
context.API.StopLoadingBar();
}
});
}
return false;
}
});
}
return results;
}

View file

@ -53,7 +53,6 @@
<Compile Include="..\..\SolutionAssemblyInfo.cs">
<Link>Properties\SolutionAssemblyInfo.cs</Link>
</Compile>
<Compile Include="HttpRequest.cs" />
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WoxPluginResult.cs" />

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Newtonsoft.Json;
@ -18,7 +19,20 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
public override async Task<List<string>> GetSuggestions(string query)
{
var result = await HttpRequest.Get("http://suggestion.baidu.com/su?json=1&wd=" + Uri.EscapeUriString(query), Proxy, "GB2312");
string result;
try
{
const string api = "http://suggestion.baidu.com/su?json=1&wd=";
result = await Http.Get(api + Uri.EscapeUriString(query), Proxy, "GB2312");
}
catch (WebException e)
{
Log.Warn("Can't get suggestion from baidu");
Log.Error(e);
return new List<string>(); ;
}
if (string.IsNullOrEmpty(result)) return new List<string>();
Match match = reg.Match(result);

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -14,7 +15,18 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
public override string Domain { get; set; } = "www.google.com";
public override async Task<List<string>> GetSuggestions(string query)
{
var result = await HttpRequest.Get("https://www.google.com/complete/search?output=chrome&q=" + Uri.EscapeUriString(query), Proxy);
string result;
try
{
const string api = "https://www.google.com/complete/search?output=chrome&q=";
result = await Http.Get(api + Uri.EscapeUriString(query), Proxy);
}
catch (WebException e)
{
Log.Warn("Can't get suggestion from google");
Log.Error(e);
return new List<string>(); ;
}
if (string.IsNullOrEmpty(result)) return new List<string>();
JContainer json;
try

View file

@ -19,7 +19,7 @@ namespace Wox.Core
public static async void UpdateApp()
{
var client = new WebClient {Proxy = HttpRequest.WebProxy(HttpProxy.Instance)};
var client = new WebClient {Proxy = Http.WebProxy(HttpProxy.Instance)};
var downloader = new FileDownloader(client);
try
@ -59,10 +59,22 @@ namespace Wox.Core
}
}
public static async Task<string> NewVersion()
{
const string githubAPI = @"https://api.github.com/repos/wox-launcher/wox/releases/latest";
var response = await HttpRequest.Get(githubAPI, HttpProxy.Instance);
string response;
try
{
response = await Http.Get(githubAPI, HttpProxy.Instance);
}
catch (WebException e)
{
Log.Warn("Can't connect to github api to check new version");
Log.Error(e);
return string.Empty;
}
if (!string.IsNullOrEmpty(response))
{

View file

@ -1,17 +1,13 @@
using System;
using System.IO;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Wox.Infrastructure.Logger;
using Wox.Plugin;
namespace Wox.Infrastructure.Http
{
public static class HttpRequest
public static class Http
{
public static WebProxy WebProxy(IHttpProxy proxy)
{
@ -37,6 +33,14 @@ namespace Wox.Infrastructure.Http
}
}
/// <exception cref="WebException">Can't download file </exception>
public static void Download([NotNull] string url, [NotNull] string filePath, IHttpProxy proxy)
{
var client = new WebClient { Proxy = WebProxy(proxy) };
client.DownloadFile(url, filePath);
}
/// <exception cref="WebException">Can't get response from http get </exception>
public static async Task<string> Get([NotNull] string url, IHttpProxy proxy, string encoding = "UTF-8")
{
@ -45,16 +49,7 @@ namespace Wox.Infrastructure.Http
request.Timeout = 10 * 1000;
request.Proxy = WebProxy(proxy);
request.UserAgent = @"Mozilla/5.0 (Trident/7.0; rv:11.0) like Gecko";
HttpWebResponse response;
try
{
response = await request.GetResponseAsync() as HttpWebResponse;
}
catch (WebException e)
{
Log.Error(e);
return string.Empty;
}
var response = await request.GetResponseAsync() as HttpWebResponse;
if (response != null)
{
var stream = response.GetResponseStream();