Droptext.cc – Free Anonymous Text Hosting

Share your text data anonymously and free



using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.Json;

namespace ApiClient
{
public static class ApiClient
{
public static string CallExternalApi(string url, string api, string filter = "")
{
string apiUrl = $"{url}/{api}";
if (!string.IsNullOrEmpty(filter))
{
apiUrl += $"?{filter}";
}

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl);
request.Method = "GET";
request.ContentType = "application/json";

try
{
using (WebResponse response = request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
string responseJson = reader.ReadToEnd();
return responseJson;
}
}
}
}
catch (WebException ex)
{
using (WebResponse response = ex.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
using (Stream data = response.GetResponseStream())
{
string text = new StreamReader(data).ReadToEnd();
throw new Exception(text);
}
}
}
}
}
}



Views: 11, posted on: 2023-02-27