Refactor the utility method to download a string

Also fix exceptional resource leak CID18224
This commit is contained in:
_aLfa_ 2014-09-26 19:31:01 +02:00
parent df4b30379e
commit a1c753818f
2 changed files with 15 additions and 23 deletions

View File

@ -378,13 +378,10 @@ namespace Nikse.SubtitleEdit.Forms
public static Encoding GetScreenScrapingEncoding(string languagePair) public static Encoding GetScreenScrapingEncoding(string languagePair)
{ {
WebClient webClient = null;
try 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"); 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(); var result = Utilities.DownloadString(url).ToLower();
webClient.Proxy = Utilities.GetProxy();
string result = webClient.DownloadString(url).ToLower();
int idx = result.IndexOf("charset", StringComparison.Ordinal); int idx = result.IndexOf("charset", StringComparison.Ordinal);
int end = result.IndexOf('"', idx + 8); int end = result.IndexOf('"', idx + 8);
string charset = result.Substring(idx, end - idx).Replace("charset=", string.Empty); string charset = result.Substring(idx, end - idx).Replace("charset=", string.Empty);
@ -394,13 +391,6 @@ namespace Nikse.SubtitleEdit.Forms
{ {
return Encoding.Default; return Encoding.Default;
} }
finally
{
if (webClient != null)
{
webClient.Dispose();
}
}
} }
/// <summary> /// <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://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)); 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(); var result = Utilities.DownloadString(url);
webClient.Proxy = Utilities.GetProxy();
webClient.Encoding = encoding;
string result = webClient.DownloadString(url);
webClient.Dispose();
int startIndex = result.IndexOf("<span id=result_box", StringComparison.Ordinal); int startIndex = result.IndexOf("<span id=result_box", StringComparison.Ordinal);
var sb = new StringBuilder(); var sb = new StringBuilder();
if (startIndex > 0) if (startIndex > 0)

View File

@ -306,12 +306,18 @@ namespace Nikse.SubtitleEdit.Logic
return -1; 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() }; using (var wc = new WebClient())
var ms = new MemoryStream(wc.DownloadData(url)); {
var reader = new StreamReader(ms); wc.Proxy = GetProxy();
return reader.ReadToEnd().Trim(); return wc.DownloadString(address).Trim();
}
} }
public static WebProxy GetProxy() public static WebProxy GetProxy()
@ -2118,7 +2124,7 @@ namespace Nikse.SubtitleEdit.Logic
{ {
try try
{ {
string xml = ReadTextFileViaUrlAndProxyIfAvailable(Configuration.Settings.WordLists.NamesEtcUrl); var xml = DownloadString(Configuration.Settings.WordLists.NamesEtcUrl);
namesEtcDoc.LoadXml(xml); namesEtcDoc.LoadXml(xml);
loaded = true; loaded = true;
} }
@ -2157,7 +2163,7 @@ namespace Nikse.SubtitleEdit.Logic
{ {
try try
{ {
string xml = ReadTextFileViaUrlAndProxyIfAvailable(Configuration.Settings.WordLists.NamesEtcUrl); var xml = DownloadString(Configuration.Settings.WordLists.NamesEtcUrl);
namesEtcDoc.LoadXml(xml); namesEtcDoc.LoadXml(xml);
loaded = true; loaded = true;
} }