BUG-1850: Fixing disposing of WebResponse objects, which might cause the greenshot not responding issue

This commit is contained in:
Robin 2015-10-29 12:22:45 +01:00
parent f92dd4122c
commit 06ae3d4901
2 changed files with 80 additions and 38 deletions

View file

@ -83,10 +83,15 @@ namespace Greenshot.Help
private static HttpStatusCode? GetHttpStatus(string url) { private static HttpStatusCode? GetHttpStatus(string url) {
try { try {
HttpWebRequest req = NetworkHelper.CreateWebRequest(url); HttpWebRequest req = NetworkHelper.CreateWebRequest(url);
HttpWebResponse res = (HttpWebResponse)req.GetResponse(); using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
return res.StatusCode; return res.StatusCode;
} catch(WebException e) { }
if(e.Response != null) return ((HttpWebResponse)e.Response).StatusCode; } catch (WebException e) {
if (e.Response != null)
{
return ((HttpWebResponse)e.Response).StatusCode;
}
return null; return null;
} }
} }

View file

@ -77,16 +77,22 @@ namespace GreenshotPlugin.Core {
Uri url = new Uri(baseUri, new Uri("favicon.ico")); Uri url = new Uri(baseUri, new Uri("favicon.ico"));
try { try {
HttpWebRequest request = CreateWebRequest(url); HttpWebRequest request = CreateWebRequest(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
if (request.HaveResponse) { {
using (Stream responseStream = response.GetResponseStream()) { if (request.HaveResponse)
if (responseStream != null) { {
using (Image image = Image.FromStream(responseStream)) { using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (Image image = Image.FromStream(responseStream))
{
return (image.Height > 16 && image.Width > 16) ? new Bitmap(image, 16, 16) : new Bitmap(image); return (image.Height > 16 && image.Width > 16) ? new Bitmap(image, 16, 16) : new Bitmap(image);
} }
} }
} }
} }
}
} catch (Exception e) { } catch (Exception e) {
LOG.Error("Problem downloading the FavIcon from: " + baseUri, e); LOG.Error("Problem downloading the FavIcon from: " + baseUri, e);
} }
@ -424,6 +430,32 @@ namespace GreenshotPlugin.Core {
return GetResponseAsString(webRequest, false); return GetResponseAsString(webRequest, false);
} }
/// <summary>
/// Read the response as string
/// </summary>
/// <param name="response"></param>
/// <returns>string or null</returns>
private static string GetResponseAsString(HttpWebResponse response)
{
string responseData = null;
if (response == null)
{
return null;
}
using (response)
{
Stream responseStream = response.GetResponseStream();
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream, true))
{
responseData = reader.ReadToEnd();
}
}
}
return responseData;
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -431,40 +463,43 @@ namespace GreenshotPlugin.Core {
/// <returns></returns> /// <returns></returns>
public static string GetResponseAsString(HttpWebRequest webRequest, bool alsoReturnContentOnError) { public static string GetResponseAsString(HttpWebRequest webRequest, bool alsoReturnContentOnError) {
string responseData = null; string responseData = null;
HttpWebResponse response = null;
bool isHttpError = false;
try { try {
HttpWebResponse response = (HttpWebResponse) webRequest.GetResponse(); response = (HttpWebResponse)webRequest.GetResponse();
LOG.InfoFormat("Response status: {0}", response.StatusCode); LOG.InfoFormat("Response status: {0}", response.StatusCode);
bool isHttpError = (int) response.StatusCode >= 300; isHttpError = (int)response.StatusCode >= 300;
DebugHeaders(response); if (isHttpError)
Stream responseStream = response.GetResponseStream(); {
if (responseStream != null) {
using (StreamReader reader = new StreamReader(responseStream, true)) {
responseData = reader.ReadToEnd();
}
if (isHttpError) {
LOG.ErrorFormat("HTTP error {0} with content: {1}", response.StatusCode, responseData); LOG.ErrorFormat("HTTP error {0} with content: {1}", response.StatusCode, responseData);
} }
} DebugHeaders(response);
responseData = GetResponseAsString(response);
} catch (WebException e) { } catch (WebException e) {
HttpWebResponse response = (HttpWebResponse) e.Response; response = (HttpWebResponse) e.Response;
if (response != null) { if (response != null) {
LOG.ErrorFormat("HTTP error {0}", response.StatusCode); LOG.ErrorFormat("HTTP error {0}", response.StatusCode);
using (Stream responseStream = response.GetResponseStream()) { string errorContent = GetResponseAsString(response);
if (responseStream != null) { if (alsoReturnContentOnError)
using (StreamReader streamReader = new StreamReader(responseStream, true)) { {
string errorContent = streamReader.ReadToEnd();
if (alsoReturnContentOnError) {
return errorContent; return errorContent;
} }
LOG.ErrorFormat("Content: {0}", errorContent); LOG.ErrorFormat("Content: {0}", errorContent);
} }
}
}
}
LOG.Error("WebException: ", e); LOG.Error("WebException: ", e);
throw; throw;
} }
finally
{
if (response != null)
{
if (isHttpError)
{
LOG.ErrorFormat("HTTP error {0} with content: {1}", response.StatusCode, responseData);
}
response.Close();
}
}
return responseData; return responseData;
} }
@ -477,9 +512,11 @@ namespace GreenshotPlugin.Core {
try { try {
HttpWebRequest webRequest = CreateWebRequest(uri); HttpWebRequest webRequest = CreateWebRequest(uri);
webRequest.Method = HTTPMethod.HEAD.ToString(); webRequest.Method = HTTPMethod.HEAD.ToString();
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
LOG.DebugFormat("RSS feed was updated at {0}", webResponse.LastModified); LOG.DebugFormat("RSS feed was updated at {0}", webResponse.LastModified);
return webResponse.LastModified; return webResponse.LastModified;
}
} catch (Exception wE) { } catch (Exception wE) {
LOG.WarnFormat("Problem requesting HTTP - HEAD on uri {0}", uri); LOG.WarnFormat("Problem requesting HTTP - HEAD on uri {0}", uri);
LOG.Warn(wE.Message); LOG.Warn(wE.Message);