Skip to content

Instantly share code, notes, and snippets.

@RoyAwesome
Last active August 29, 2015 14:01
Show Gist options
  • Save RoyAwesome/c4157b98a1010322f3ad to your computer and use it in GitHub Desktop.
Save RoyAwesome/c4157b98a1010322f3ad to your computer and use it in GitHub Desktop.
Hastebin poster
private static readonly Regex HasteKeyRegex = new Regex(@"{""key"":""(?<key>[a-z].*)""}", RegexOptions.Compiled);
public static string Haste(this string message)
{
string hastebin = @"http://hastebin.com/";
using (WebClient client = new WebClient())
{
string response;
try
{
response = client.UploadString(hastebin + "documents", message);
}
catch (System.Net.WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.ServiceUnavailable)
{
return "Error: string was too big for hastebin!";
}
else if(((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.NotFound)
{
return "Error: Page not found";
}
}
throw e;
}
var match = HasteKeyRegex.Match(response);
if (!match.Success)
{
return "Error: " + response;
}
return hastebin + match.Groups["key"];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment