Skip to content

Instantly share code, notes, and snippets.

@zmilojko
Last active May 11, 2020 03:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zmilojko/5756502 to your computer and use it in GitHub Desktop.
Save zmilojko/5756502 to your computer and use it in GitHub Desktop.
Here is how you can make C# WebRequests asynchronously. The version on MSDN is overcomplicated and utterly sucks. This one is minimal and doesn't suck.
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Guoxun Yang and Zeljko Milojkovic wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we meet some day, and
* you think this stuff is worth it, you can buy us beer in return.
* ----------------------------------------------------------------------------
*/
using System;
using System.Net;
// Reference needed: System
// Target framework 2.0 or higher, Client or not
// Output type: Console application (for the Program class only)
namespace GYZM
{
class AsyncWebRequest
{
static public AsyncWebRequest Create(string Url)
{
return new AsyncWebRequest(new byte[BUFFER_SIZE],
(HttpWebRequest)WebRequest.Create(Url));
}
AsyncWebRequest(byte[] buffer, HttpWebRequest request)
{
this.buffer = buffer;
this.request = request;
}
const int BUFFER_SIZE = 4096;
readonly HttpWebRequest request;
readonly byte[] buffer;
HttpWebResponse response;
string result;
public delegate void ResultDelegate(string result);
public delegate void ErrorDelegate(Exception e);
public void Execute(ResultDelegate OnComplete, ErrorDelegate OnError)
{
request.BeginGetResponse(new AsyncCallback(async_result_get_response =>
{
try
{
result = "";
response = (HttpWebResponse)request.EndGetResponse(async_result_get_response);
Read(OnComplete, OnError);
}
catch (Exception ex)
{
OnError(ex);
return;
}
}), this);
}
void Read(ResultDelegate OnComplete, ErrorDelegate OnError)
{
response.GetResponseStream().BeginRead(buffer, 0, BUFFER_SIZE, async_results_read =>
{
int count = response.GetResponseStream().EndRead(async_results_read);
if (count > 0)
{
result += System.Text.Encoding.ASCII.GetString(buffer, 0, count);
Read(OnComplete, OnError);
}
else
{
response.GetResponseStream().Close();
response.Close();
OnComplete(result);
}
}, this);
}
}
class Program
{
static void Main(string[] args)
{
string address;
while (string.IsNullOrWhiteSpace(address = Console.ReadLine()) || address.ToLower() != "exit")
{
if (!address.Contains("http://"))
{
address = "http://" + address;
}
Console.WriteLine("GET " + address);
AsyncWebRequest.Create(address).Execute(result_text =>
{
Console.WriteLine();
Console.WriteLine("Received response:");
Console.WriteLine();
Console.WriteLine(result_text);
Console.WriteLine();
Console.WriteLine();
},
error =>
{
Console.WriteLine("Request failed for the following reason: " + error.Message);
Console.WriteLine();
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment