Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HttpRequestInputStreamDispose
[TestFixture]
public class HttpListenerRequestTest
{
[Test]
public void HttpRequestInputStreamDispose()
{
var uri = "http://localhost:8083/";
var requestContent = "content=request";
HttpListener listener = new HttpListener();
listener.Prefixes.Add(uri);
listener.Start();
for (var i = 0; i < 20; i++)
{
var clientTask = listener.GetContextAsync();
var request1 = (HttpWebRequest)WebRequest.Create(uri);
request1.Method = "POST";
using (var writer = new StreamWriter(request1.GetRequestStream(), Encoding.ASCII))
writer.Write(requestContent);
request1.GetResponseAsync();
Assert.IsTrue(clientTask.Wait(1000));
AcceptClient(clientTask.Result);
responseTask.Result.Close();
}
}
public void AcceptClient(HttpListenerContext client)
{
string sRequest;
using (var reader = new StreamReader(client.Request.InputStream, Encoding.ASCII))
sRequest = reader.ReadToEnd();
client.Request.InputStream.Dispose();
byte[] byteArray = Encoding.UTF8.GetBytes("response");
client.Response.ContentLength64 = byteArray.Length;
client.Response.OutputStream.Write(byteArray, 0, byteArray.Length);
client.Response.OutputStream.Close();
client.Response.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment