Skip to content

Commit e867b31

Browse files
nvborisenkojimevans
authored andcommitted
Dispose HttpRequestMessage and HttpResponseMessage objects when making a http call
Signed-off-by: Jim Evans <james.h.evans.jr@gmail.com>
1 parent 57b5345 commit e867b31

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

dotnet/src/webdriver/Remote/HttpCommandExecutor.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public virtual Response Execute(Command commandToExecute)
193193
string unknownErrorMessage = "An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL {0}. The exception message was: {1}";
194194
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, unknownErrorMessage, requestInfo.FullUri.AbsoluteUri, ex.Message), ex);
195195
}
196-
catch(TaskCanceledException ex)
196+
catch (TaskCanceledException ex)
197197
{
198198
string timeoutMessage = "The HTTP request to the remote WebDriver server for URL {0} timed out after {1} seconds.";
199199
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, timeoutMessage, requestInfo.FullUri.AbsoluteUri, this.serverResponseTimeout.TotalSeconds), ex);
@@ -253,34 +253,39 @@ private async Task<HttpResponseInfo> MakeHttpRequest(HttpRequestInfo requestInfo
253253
this.OnSendingRemoteHttpRequest(eventArgs);
254254

255255
HttpMethod method = new HttpMethod(requestInfo.HttpMethod);
256-
HttpRequestMessage requestMessage = new HttpRequestMessage(method, requestInfo.FullUri);
257-
if (requestInfo.HttpMethod == HttpCommandInfo.GetCommand)
256+
using (HttpRequestMessage requestMessage = new HttpRequestMessage(method, requestInfo.FullUri))
258257
{
259-
CacheControlHeaderValue cacheControlHeader = new CacheControlHeaderValue();
260-
cacheControlHeader.NoCache = true;
261-
requestMessage.Headers.CacheControl = cacheControlHeader;
262-
}
258+
if (requestInfo.HttpMethod == HttpCommandInfo.GetCommand)
259+
{
260+
CacheControlHeaderValue cacheControlHeader = new CacheControlHeaderValue();
261+
cacheControlHeader.NoCache = true;
262+
requestMessage.Headers.CacheControl = cacheControlHeader;
263+
}
263264

264-
if (requestInfo.HttpMethod == HttpCommandInfo.PostCommand)
265-
{
266-
MediaTypeWithQualityHeaderValue acceptHeader = new MediaTypeWithQualityHeaderValue(JsonMimeType);
267-
acceptHeader.CharSet = Utf8CharsetType;
268-
requestMessage.Headers.Accept.Add(acceptHeader);
265+
if (requestInfo.HttpMethod == HttpCommandInfo.PostCommand)
266+
{
267+
MediaTypeWithQualityHeaderValue acceptHeader = new MediaTypeWithQualityHeaderValue(JsonMimeType);
268+
acceptHeader.CharSet = Utf8CharsetType;
269+
requestMessage.Headers.Accept.Add(acceptHeader);
269270

270-
byte[] bytes = Encoding.UTF8.GetBytes(eventArgs.RequestBody);
271-
requestMessage.Content = new ByteArrayContent(bytes, 0, bytes.Length);
271+
byte[] bytes = Encoding.UTF8.GetBytes(eventArgs.RequestBody);
272+
requestMessage.Content = new ByteArrayContent(bytes, 0, bytes.Length);
272273

273-
MediaTypeHeaderValue contentTypeHeader = new MediaTypeHeaderValue(JsonMimeType);
274-
contentTypeHeader.CharSet = Utf8CharsetType;
275-
requestMessage.Content.Headers.ContentType = contentTypeHeader;
276-
}
274+
MediaTypeHeaderValue contentTypeHeader = new MediaTypeHeaderValue(JsonMimeType);
275+
contentTypeHeader.CharSet = Utf8CharsetType;
276+
requestMessage.Content.Headers.ContentType = contentTypeHeader;
277+
}
278+
279+
using (HttpResponseMessage responseMessage = await this.client.SendAsync(requestMessage))
280+
{
281+
HttpResponseInfo httpResponseInfo = new HttpResponseInfo();
282+
httpResponseInfo.Body = await responseMessage.Content.ReadAsStringAsync();
283+
httpResponseInfo.ContentType = responseMessage.Content.Headers.ContentType.ToString();
284+
httpResponseInfo.StatusCode = responseMessage.StatusCode;
277285

278-
HttpResponseMessage responseMessage = await this.client.SendAsync(requestMessage);
279-
HttpResponseInfo httpResponseInfo = new HttpResponseInfo();
280-
httpResponseInfo.Body = await responseMessage.Content.ReadAsStringAsync();
281-
httpResponseInfo.ContentType = responseMessage.Content.Headers.ContentType.ToString();
282-
httpResponseInfo.StatusCode = responseMessage.StatusCode;
283-
return httpResponseInfo;
286+
return httpResponseInfo;
287+
}
288+
}
284289
}
285290

286291
private Response CreateResponse(HttpResponseInfo responseInfo)

0 commit comments

Comments
 (0)