Created
January 6, 2015 23:59
-
-
Save alexandrnikitin/86b3e5a517455f7ff8b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace HttpClientMemoryLeak | |
{ | |
class Program | |
{ | |
private static CookieContainer _Cookies = new CookieContainer(); | |
public static async Task TestMethod() | |
{ | |
using (HttpClientHandler handler = new HttpClientHandler()) | |
{ | |
handler.UseCookies = true; | |
handler.CookieContainer = _Cookies; | |
using (HttpClient client = new HttpClient(handler, true)) | |
{ | |
client.Timeout = new TimeSpan(0, 0, 1); | |
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "asd"); | |
try | |
{ | |
using (StringContent sData = new StringContent("hello", Encoding.UTF8)) | |
using (HttpResponseMessage response = await client.PutAsync("https://localhost/", sData)) | |
{ | |
using (var content = response.Content) | |
{ | |
var ret = await content.ReadAsStringAsync(); | |
} | |
} | |
} | |
catch (ThreadAbortException) | |
{ | |
throw; | |
} | |
catch (Exception ex) | |
{ | |
} | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
for (int i = 0; i < 1000000; i++) | |
{ | |
TestMethod().Wait(); | |
} | |
Console.WriteLine("Finished!"); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment