Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Last active April 18, 2023 06:27
Show Gist options
  • Save yetanotherchris/b5e14d0c36f5a972060655b1aa875dbf to your computer and use it in GitHub Desktop.
Save yetanotherchris/b5e14d0c36f5a972060655b1aa875dbf to your computer and use it in GitHub Desktop.
A Hastebin client in C#
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; }
}
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\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