Skip to content

Instantly share code, notes, and snippets.

@alanmcgovern
Created January 7, 2016 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanmcgovern/88fd898a4ae7dd669684 to your computer and use it in GitHub Desktop.
Save alanmcgovern/88fd898a4ae7dd669684 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace webrasher
{
class MainClass
{
public static void Main(string[] args)
{
var server = Server ();
System.Threading.Thread.Sleep (1000);
var client = Task.Run (Client);
server.Wait ();
client.Wait ();
}
public static async Task Server ()
{
var server = new System.Net.HttpListener ();
server.Prefixes.Add ("http://*:12345/");
server.Start ();
while (true) {
var context = await server.GetContextAsync ();
Console.WriteLine (new StreamReader (context.Request.InputStream).ReadToEnd ());
using (context.Response) {
new StreamWriter (context.Response.OutputStream).Write ("Hello World");
}
}
}
public static void Client ()
{
for (int i = 0; i < 100; i ++) {
var client = WebRequest.CreateHttp ("http://127.0.0.1:12345/");
client.Method = "POST";
using (var request = client.GetRequestStream ())
request.Write (Encoding.UTF8.GetBytes ("Hello"), 0, "Hello".Length);
var asyncResult = client.BeginGetResponse (null, null);
client.Abort ();
try {
var finalResult = client.EndGetResponse (asyncResult);
} catch (WebException) {
// Good!
} catch (ObjectDisposedException) {
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment