Skip to content

Instantly share code, notes, and snippets.

@umerfaruk
Created July 8, 2020 08:39
Show Gist options
  • Save umerfaruk/b6d76127e10b32daffd1003fcb2b1c71 to your computer and use it in GitHub Desktop.
Save umerfaruk/b6d76127e10b32daffd1003fcb2b1c71 to your computer and use it in GitHub Desktop.
HttpHelper
public static async Task<string> GetTextBody(string url)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "GET";
WebResponse myResponse = await myRequest.GetResponseAsync();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string result = await sr.ReadToEndAsync();
sr.Close();
myResponse.Close();
return result;
}
public static async Task<Bitmap> GetImage(string imageUrl)
{
var webRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
webRequest.AllowWriteStreamBuffering = true;
webRequest.Timeout = 30000;
using (var webResponse = await webRequest.GetResponseAsync())
{
using (var stream = webResponse.GetResponseStream())
{
return new Bitmap(stream);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment