-
-
Save Zorono/d262955c9b84ac2e859389798da3d0e5 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.
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
/* | |
* ---------------------------------------------------------------------------- | |
* "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