Skip to content

Instantly share code, notes, and snippets.

@GentlemanHal
Created December 17, 2013 11:19
Show Gist options
  • Save GentlemanHal/8003419 to your computer and use it in GitHub Desktop.
Save GentlemanHal/8003419 to your computer and use it in GitHub Desktop.
Perform a simple connection attempt in .net
using System.Net;
public class Test
{
static void Main()
{
WebRequest request = WebRequest.Create("url here");
try
{
using (WebResponse response = request.GetResponse())
{
Console.WriteLine("Connected!");
}
}
catch (WebException e)
{
using (WebResponse response = e.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse) response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream data = response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment