-
-
Save alanmcgovern/88fd898a4ae7dd669684 to your computer and use it in GitHub Desktop.
This file contains 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.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