mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-26 05:02:36 +01:00
Refactor the utility method to download a string
Also fix exceptional resource leak CID18224
This commit is contained in:
parent
df4b30379e
commit
a1c753818f
@ -378,13 +378,10 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
|
||||
public static Encoding GetScreenScrapingEncoding(string languagePair)
|
||||
{
|
||||
WebClient webClient = null;
|
||||
try
|
||||
{
|
||||
string url = String.Format("http://translate.google.com/?hl=en&eotf=1&sl={0}&tl={1}&q={2}", languagePair.Substring(0, 2), languagePair.Substring(3), "123 456");
|
||||
webClient = new WebClient();
|
||||
webClient.Proxy = Utilities.GetProxy();
|
||||
string result = webClient.DownloadString(url).ToLower();
|
||||
var result = Utilities.DownloadString(url).ToLower();
|
||||
int idx = result.IndexOf("charset", StringComparison.Ordinal);
|
||||
int end = result.IndexOf('"', idx + 8);
|
||||
string charset = result.Substring(idx, end - idx).Replace("charset=", string.Empty);
|
||||
@ -394,13 +391,6 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
{
|
||||
return Encoding.Default;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (webClient != null)
|
||||
{
|
||||
webClient.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -418,11 +408,7 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
|
||||
//string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", HttpUtility.UrlEncode(input), languagePair);
|
||||
string url = String.Format("http://translate.google.com/?hl=en&eotf=1&sl={0}&tl={1}&q={2}", languagePair.Substring(0, 2), languagePair.Substring(3), Utilities.UrlEncode(input));
|
||||
var webClient = new WebClient();
|
||||
webClient.Proxy = Utilities.GetProxy();
|
||||
webClient.Encoding = encoding;
|
||||
string result = webClient.DownloadString(url);
|
||||
webClient.Dispose();
|
||||
var result = Utilities.DownloadString(url);
|
||||
int startIndex = result.IndexOf("<span id=result_box", StringComparison.Ordinal);
|
||||
var sb = new StringBuilder();
|
||||
if (startIndex > 0)
|
||||
|
@ -306,12 +306,18 @@ namespace Nikse.SubtitleEdit.Logic
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static string ReadTextFileViaUrlAndProxyIfAvailable(string url)
|
||||
/// <summary>
|
||||
/// Downloads the requested resource as a <see cref="String"/> using the configured <see cref="WebProxy"/>.
|
||||
/// </summary>
|
||||
/// <param name="address">A <see cref="String"/> containing the URI to download.</param>
|
||||
/// <returns>A <see cref="String"/> containing the requested resource.</returns>
|
||||
public static string DownloadString(string address)
|
||||
{
|
||||
var wc = new WebClient { Proxy = GetProxy() };
|
||||
var ms = new MemoryStream(wc.DownloadData(url));
|
||||
var reader = new StreamReader(ms);
|
||||
return reader.ReadToEnd().Trim();
|
||||
using (var wc = new WebClient())
|
||||
{
|
||||
wc.Proxy = GetProxy();
|
||||
return wc.DownloadString(address).Trim();
|
||||
}
|
||||
}
|
||||
|
||||
public static WebProxy GetProxy()
|
||||
@ -2118,7 +2124,7 @@ namespace Nikse.SubtitleEdit.Logic
|
||||
{
|
||||
try
|
||||
{
|
||||
string xml = ReadTextFileViaUrlAndProxyIfAvailable(Configuration.Settings.WordLists.NamesEtcUrl);
|
||||
var xml = DownloadString(Configuration.Settings.WordLists.NamesEtcUrl);
|
||||
namesEtcDoc.LoadXml(xml);
|
||||
loaded = true;
|
||||
}
|
||||
@ -2157,7 +2163,7 @@ namespace Nikse.SubtitleEdit.Logic
|
||||
{
|
||||
try
|
||||
{
|
||||
string xml = ReadTextFileViaUrlAndProxyIfAvailable(Configuration.Settings.WordLists.NamesEtcUrl);
|
||||
var xml = DownloadString(Configuration.Settings.WordLists.NamesEtcUrl);
|
||||
namesEtcDoc.LoadXml(xml);
|
||||
loaded = true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user