Skip to content

Instantly share code, notes, and snippets.

@HenrikFrystykNielsen
Created June 27, 2012 14:07
Show Gist options
  • Save HenrikFrystykNielsen/3004270 to your computer and use it in GitHub Desktop.
Save HenrikFrystykNielsen/3004270 to your computer and use it in GitHub Desktop.
Asynchronous HTML form data submit using async/await in .NET 4.5 and FormUrlEncodedContent
using System;
using System.Collections.Generic;
using System.Net.Http;
namespace CardStatus
{
class Program
{
static async void RunCheck(int antragsnummer)
{
// Create client
var client = new HttpClient();
// Create HTML form data
FormUrlEncodedContent content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "COMMAND", "CHECK" },
{ "Nr", antragsnummer.ToString() }
});
// Submit request and get response asynchronously
HttpResponseMessage response = await client.PostAsync("http://duesseldorf.dokument-abholen.de/index.php", content);
// Check whether we got an OK response
if (response.IsSuccessStatusCode)
{
// Read response data asynchronously
string result = await response.Content.ReadAsStringAsync();
if (result.Contains("leider noch nicht"))
{
Console.WriteLine("Grmpf... Leider noch nicht da");
}
else
{
Console.WriteLine("Yippie ... Perso holen gehen...");
}
}
else
{
Console.WriteLine("Could not download data");
}
}
static void Main(string[] args)
{
RunCheck(12345);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment