Last active
August 29, 2015 14:01
-
-
Save RoyAwesome/c4157b98a1010322f3ad to your computer and use it in GitHub Desktop.
Hastebin poster
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
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