-
-
Save jwoff78/f8babb48922132ea20d37ef4aa8aa1dd to your computer and use it in GitHub Desktop.
A Hastebin client in C#
This file contains 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
void Main() | |
{ | |
// To run Hastebin in Docker: | |
// docker run --name pastebin -p 801:80 mkodockx/docker-pastebin | |
string baseUrl = "http://localhost:801/"; | |
var hasteBinClient = new HasteBinClient(baseUrl); | |
HasteBinResult result = hasteBinClient.Post("Hello hastebin").Result; | |
if (result.IsSuccess) | |
{ | |
Console.WriteLine($"{baseUrl}{result.Key}"); | |
} | |
else | |
{ | |
Console.WriteLine($"Failed, status code was {result.StatusCode}"); | |
} | |
} | |
public class HasteBinClient | |
{ | |
private static HttpClient _httpClient; | |
private string _baseUrl; | |
static HasteBinClient() | |
{ | |
_httpClient = new HttpClient(); | |
} | |
public HasteBinClient(string baseUrl) | |
{ | |
_baseUrl = baseUrl; | |
} | |
public async Task<HasteBinResult> Post(string content) | |
{ | |
string fullUrl = _baseUrl; | |
if (!fullUrl.EndsWith("/")) | |
{ | |
fullUrl += "/"; | |
} | |
string postUrl = $"{fullUrl}documents"; | |
var request = new HttpRequestMessage(HttpMethod.Post, new Uri(postUrl)); | |
request.Content = new StringContent(content); | |
HttpResponseMessage result = await _httpClient.SendAsync(request); | |
if (result.IsSuccessStatusCode) | |
{ | |
string json = await result.Content.ReadAsStringAsync(); | |
HasteBinResult hasteBinResult = JsonConvert.DeserializeObject<HasteBinResult>(json); | |
if (hasteBinResult?.Key != null) | |
{ | |
hasteBinResult.FullUrl = $"{fullUrl}{hasteBinResult.Key}"; | |
hasteBinResult.IsSuccess = true; | |
hasteBinResult.StatusCode = 200; | |
return hasteBinResult; | |
} | |
} | |
return new HasteBinResult() | |
{ | |
FullUrl = fullUrl, | |
IsSuccess = false, | |
StatusCode = (int) result.StatusCode | |
}; | |
} | |
} | |
// Define other methods and classes here | |
public class HasteBinResult | |
{ | |
public string Key { get; set; } | |
public string FullUrl { get; set; } | |
public bool IsSuccess { get; set; } | |
public int StatusCode { get; set; } | |
} |
This file contains 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
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference> | |
<NuGetReference>Newtonsoft.Json</NuGetReference> | |
<Namespace>System.Net.Http</Namespace> | |
<Namespace>Newtonsoft.Json</Namespace> | |
<Namespace>System.Threading.Tasks</Namespace> | |
</Query> | |
void Main() | |
{ | |
string baseUrl = "http://localhost:801/"; | |
var hasteBinClient = new HasteBinClient(baseUrl); | |
HasteBinResult result = hasteBinClient.Post("Hello hastebin").Result; | |
if (result.IsSuccess) | |
{ | |
Console.WriteLine($"{baseUrl}{result.Key}"); | |
} | |
else | |
{ | |
Console.WriteLine($"Failed, status code was {result.StatusCode}"); | |
} | |
} | |
public class HasteBinClient | |
{ | |
private static HttpClient _httpClient; | |
private string _baseUrl; | |
static HasteBinClient() | |
{ | |
_httpClient = new HttpClient(); | |
} | |
public HasteBinClient(string baseUrl) | |
{ | |
_baseUrl = baseUrl; | |
} | |
public async Task<HasteBinResult> Post(string content) | |
{ | |
string fullUrl = _baseUrl; | |
if (!fullUrl.EndsWith("/")) | |
{ | |
fullUrl += "/"; | |
} | |
string postUrl = $"{fullUrl}documents"; | |
var request = new HttpRequestMessage(HttpMethod.Post, new Uri(postUrl)); | |
request.Content = new StringContent(content); | |
HttpResponseMessage result = await _httpClient.SendAsync(request); | |
if (result.IsSuccessStatusCode) | |
{ | |
string json = await result.Content.ReadAsStringAsync(); | |
HasteBinResult hasteBinResult = JsonConvert.DeserializeObject<HasteBinResult>(json); | |
if (hasteBinResult?.Key != null) | |
{ | |
hasteBinResult.FullUrl = $"{fullUrl}{hasteBinResult.Key}"; | |
hasteBinResult.IsSuccess = true; | |
hasteBinResult.StatusCode = 200; | |
return hasteBinResult; | |
} | |
} | |
return new HasteBinResult() | |
{ | |
FullUrl = fullUrl, | |
IsSuccess = false, | |
StatusCode = (int) result.StatusCode | |
}; | |
} | |
} | |
// Define other methods and classes here | |
public class HasteBinResult | |
{ | |
public string Key { get; set; } | |
public string FullUrl { get; set; } | |
public bool IsSuccess { get; set; } | |
public int StatusCode { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment