diff --git a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs index 6d4e6b6b3..e094b1514 100644 --- a/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs +++ b/src/NzbDrone.Common/Http/Dispatchers/ManagedHttpDispatcher.cs @@ -20,72 +20,84 @@ namespace NzbDrone.Common.Http.Dispatchers public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies) { - var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url); - - // Deflate is not a standard and could break depending on implementation. - // we should just stick with the more compatible Gzip - //http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net - webRequest.AutomaticDecompression = DecompressionMethods.GZip; - - webRequest.Method = request.Method.ToString(); - webRequest.UserAgent = request.UseSimplifiedUserAgent ? UserAgentBuilder.UserAgentSimplified : UserAgentBuilder.UserAgent; - webRequest.KeepAlive = request.ConnectionKeepAlive; - webRequest.AllowAutoRedirect = false; - webRequest.CookieContainer = cookies; - - if (request.RequestTimeout != TimeSpan.Zero) + HttpWebResponse httpWebResponse = null; + HttpWebRequest webRequest = null; + try { - webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds); - } - - AddProxy(webRequest, request); + webRequest = (HttpWebRequest) WebRequest.Create((Uri) request.Url); + + // Deflate is not a standard and could break depending on implementation. + // we should just stick with the more compatible Gzip + //http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net + webRequest.AutomaticDecompression = DecompressionMethods.GZip; + + webRequest.Method = request.Method.ToString(); + webRequest.UserAgent = request.UseSimplifiedUserAgent + ? UserAgentBuilder.UserAgentSimplified + : UserAgentBuilder.UserAgent; + webRequest.KeepAlive = request.ConnectionKeepAlive; + webRequest.AllowAutoRedirect = false; + webRequest.CookieContainer = cookies; + + if (request.RequestTimeout != TimeSpan.Zero) + { + webRequest.Timeout = (int) Math.Ceiling(request.RequestTimeout.TotalMilliseconds); + } - if (request.Headers != null) - { - AddRequestHeaders(webRequest, request.Headers); - } + AddProxy(webRequest, request); - if (request.ContentData != null) - { - webRequest.ContentLength = request.ContentData.Length; - using (var writeStream = webRequest.GetRequestStream()) + if (request.Headers != null) { - writeStream.Write(request.ContentData, 0, request.ContentData.Length); + AddRequestHeaders(webRequest, request.Headers); } - } - HttpWebResponse httpWebResponse; + if (request.ContentData != null) + { + webRequest.ContentLength = request.ContentData.Length; + using (var writeStream = webRequest.GetRequestStream()) + { + writeStream.Write(request.ContentData, 0, request.ContentData.Length); + } + } - try - { - httpWebResponse = (HttpWebResponse)webRequest.GetResponse(); - } - catch (WebException e) - { - if (e.Status == WebExceptionStatus.SecureChannelFailure && OsInfo.IsWindows) + try { - SecurityProtocolPolicy.DisableTls12(); + httpWebResponse = (HttpWebResponse) webRequest.GetResponse(); } + catch (WebException e) + { + if (e.Status == WebExceptionStatus.SecureChannelFailure && OsInfo.IsWindows) + { + SecurityProtocolPolicy.DisableTls12(); + } - httpWebResponse = (HttpWebResponse)e.Response; + httpWebResponse = (HttpWebResponse) e.Response; - if (httpWebResponse == null) - { - throw; + if (httpWebResponse == null) + { + throw; + } } - } - byte[] data = null; + byte[] data = null; - using (var responseStream = httpWebResponse.GetResponseStream()) - { - if (responseStream != null) + using (var responseStream = httpWebResponse.GetResponseStream()) { - data = responseStream.ToBytes(); + if (responseStream != null) + { + data = responseStream.ToBytes(); + } } - } - return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, httpWebResponse.StatusCode); + return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, + httpWebResponse.StatusCode); + } + finally + { + webRequest = null; + httpWebResponse?.Dispose(); + httpWebResponse = null; + } } protected virtual void AddProxy(HttpWebRequest webRequest, HttpRequest request)