Skip to content

Commit

Permalink
Dispose HttpRequestMessage and HttpResponseMessage objects when makin…
Browse files Browse the repository at this point in the history
…g a http call

Signed-off-by: Jim Evans <james.h.evans.jr@gmail.com>
  • Loading branch information
nvborisenko authored and jimevans committed Aug 16, 2021
1 parent 57b5345 commit e867b31
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public virtual Response Execute(Command commandToExecute)
string unknownErrorMessage = "An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL {0}. The exception message was: {1}";
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, unknownErrorMessage, requestInfo.FullUri.AbsoluteUri, ex.Message), ex);
}
catch(TaskCanceledException ex)
catch (TaskCanceledException ex)
{
string timeoutMessage = "The HTTP request to the remote WebDriver server for URL {0} timed out after {1} seconds.";
throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, timeoutMessage, requestInfo.FullUri.AbsoluteUri, this.serverResponseTimeout.TotalSeconds), ex);
Expand Down Expand Up @@ -253,34 +253,39 @@ private async Task<HttpResponseInfo> MakeHttpRequest(HttpRequestInfo requestInfo
this.OnSendingRemoteHttpRequest(eventArgs);

HttpMethod method = new HttpMethod(requestInfo.HttpMethod);
HttpRequestMessage requestMessage = new HttpRequestMessage(method, requestInfo.FullUri);
if (requestInfo.HttpMethod == HttpCommandInfo.GetCommand)
using (HttpRequestMessage requestMessage = new HttpRequestMessage(method, requestInfo.FullUri))
{
CacheControlHeaderValue cacheControlHeader = new CacheControlHeaderValue();
cacheControlHeader.NoCache = true;
requestMessage.Headers.CacheControl = cacheControlHeader;
}
if (requestInfo.HttpMethod == HttpCommandInfo.GetCommand)
{
CacheControlHeaderValue cacheControlHeader = new CacheControlHeaderValue();
cacheControlHeader.NoCache = true;
requestMessage.Headers.CacheControl = cacheControlHeader;
}

if (requestInfo.HttpMethod == HttpCommandInfo.PostCommand)
{
MediaTypeWithQualityHeaderValue acceptHeader = new MediaTypeWithQualityHeaderValue(JsonMimeType);
acceptHeader.CharSet = Utf8CharsetType;
requestMessage.Headers.Accept.Add(acceptHeader);
if (requestInfo.HttpMethod == HttpCommandInfo.PostCommand)
{
MediaTypeWithQualityHeaderValue acceptHeader = new MediaTypeWithQualityHeaderValue(JsonMimeType);
acceptHeader.CharSet = Utf8CharsetType;
requestMessage.Headers.Accept.Add(acceptHeader);

byte[] bytes = Encoding.UTF8.GetBytes(eventArgs.RequestBody);
requestMessage.Content = new ByteArrayContent(bytes, 0, bytes.Length);
byte[] bytes = Encoding.UTF8.GetBytes(eventArgs.RequestBody);
requestMessage.Content = new ByteArrayContent(bytes, 0, bytes.Length);

MediaTypeHeaderValue contentTypeHeader = new MediaTypeHeaderValue(JsonMimeType);
contentTypeHeader.CharSet = Utf8CharsetType;
requestMessage.Content.Headers.ContentType = contentTypeHeader;
}
MediaTypeHeaderValue contentTypeHeader = new MediaTypeHeaderValue(JsonMimeType);
contentTypeHeader.CharSet = Utf8CharsetType;
requestMessage.Content.Headers.ContentType = contentTypeHeader;
}

using (HttpResponseMessage responseMessage = await this.client.SendAsync(requestMessage))
{
HttpResponseInfo httpResponseInfo = new HttpResponseInfo();
httpResponseInfo.Body = await responseMessage.Content.ReadAsStringAsync();
httpResponseInfo.ContentType = responseMessage.Content.Headers.ContentType.ToString();
httpResponseInfo.StatusCode = responseMessage.StatusCode;

HttpResponseMessage responseMessage = await this.client.SendAsync(requestMessage);
HttpResponseInfo httpResponseInfo = new HttpResponseInfo();
httpResponseInfo.Body = await responseMessage.Content.ReadAsStringAsync();
httpResponseInfo.ContentType = responseMessage.Content.Headers.ContentType.ToString();
httpResponseInfo.StatusCode = responseMessage.StatusCode;
return httpResponseInfo;
return httpResponseInfo;
}
}
}

private Response CreateResponse(HttpResponseInfo responseInfo)
Expand Down

0 comments on commit e867b31

Please sign in to comment.