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) {
try {
HttpWebRequest req = NetworkHelper.CreateWebRequest(url);
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
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;
}
}

View file

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