fix: catch not connect to network exception

master
ZGGSONG 2 years ago
parent d94b20f39b
commit 866d896df8

@ -61,11 +61,18 @@ namespace STranslate.Utils
{
using (var client = new HttpClient())
{
var respContent = await client.GetAsync(urlpath);
try
{
var respContent = await client.GetAsync(urlpath);
string respStr = await respContent.Content.ReadAsStringAsync();
string respStr = await respContent.Content.ReadAsStringAsync();
return respStr;
return respStr;
}
catch (Exception ex)
{
throw ex;
}
}
}
}

@ -61,16 +61,18 @@ namespace STranslate.Utils
/// <returns></returns>
public static async Task<string> TranslateBaiduAsync(string appID, string secretKey, string text, LanguageEnum target, LanguageEnum source = LanguageEnum.AUTO)
{
Random rd = new Random();
string salt = rd.Next(100000).ToString();
string sign = EncryptString(appID + text + salt + secretKey);
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "q=" + HttpUtility.UrlEncode(text);
url += "&from=" + source.ToString().ToLower();
url += "&to=" + target.ToString().ToLower();
url += "&appid=" + appID;
url += "&salt=" + salt;
url += "&sign=" + sign;
try
{
Random rd = new Random();
string salt = rd.Next(100000).ToString();
string sign = EncryptString(appID + text + salt + secretKey);
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "q=" + HttpUtility.UrlEncode(text);
url += "&from=" + source.ToString().ToLower();
url += "&to=" + target.ToString().ToLower();
url += "&appid=" + appID;
url += "&salt=" + salt;
url += "&sign=" + sign;
#if false
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
@ -84,14 +86,19 @@ namespace STranslate.Utils
myStreamReader.Close();
myResponseStream.Close();
#endif
var retString = await HttpUtil.GetAsync(url);
var resp = JsonConvert.DeserializeObject<BaiduResp>(retString);
if (resp.From != null)
var retString = await HttpUtil.GetAsync(url);
var resp = JsonConvert.DeserializeObject<BaiduResp>(retString);
if (resp.From != null)
{
return resp.TransResult[0]?.Dst;
}
return string.Empty;
}
catch (Exception ex)
{
return resp.TransResult[0]?.Dst;
throw ex;
}
return string.Empty;
}
// 计算MD5值
public static string EncryptString(string str)

@ -19,31 +19,31 @@ namespace STranslate.ViewModel
private string Text;
public MainVM()
{
try
{
//初始化界面参数
InputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription();
OutputCombo = LanguageEnumDict.Keys.ToList();
OutputComboSelected = LanguageEnum.EN.GetDescription();
//初始化界面参数
InputCombo = LanguageEnumDict.Keys.ToList();
InputComboSelected = LanguageEnum.AUTO.GetDescription();
OutputCombo = LanguageEnumDict.Keys.ToList();
OutputComboSelected = LanguageEnum.EN.GetDescription();
config = ConfigUtil.ReadConfig(ConfigPath);
config = ConfigUtil.ReadConfig(ConfigPath);
//手动复制翻译结果
CopyTranslateResultCmd = new RelayCommand((_) =>
{
return string.IsNullOrEmpty(OutputTxt) ? false : true;
}, (_) =>
{
System.Diagnostics.Debug.Print("手动复制翻译结果: " + OutputTxt);
Clipboard.SetText(OutputTxt);
});
//手动复制翻译结果
CopyTranslateResultCmd = new RelayCommand((_) =>
{
return string.IsNullOrEmpty(OutputTxt) ? false : true;
}, (_) =>
{
System.Diagnostics.Debug.Print("手动复制翻译结果: " + OutputTxt);
Clipboard.SetText(OutputTxt);
});
TranslateCmd = new RelayCommand((_) =>
{
return string.IsNullOrEmpty(InputTxt) ? false : true;
}, async (_) =>
TranslateCmd = new RelayCommand((_) =>
{
return string.IsNullOrEmpty(InputTxt) ? false : true;
}, async (_) =>
{
try
{
Text = InputTxt;
@ -63,14 +63,12 @@ namespace STranslate.ViewModel
return;
}
OutputTxt = translateResp;
Clipboard.SetText(OutputTxt);
});
}
catch (Exception ex)
{
OutputTxt = ex.Message;
}
}
catch (Exception ex)
{
OutputTxt = ex.Message;
}
});
}
public ICommand TranslateCmd { get; private set; }