Created
December 17, 2013 11:19
-
-
Save GentlemanHal/8003419 to your computer and use it in GitHub Desktop.
Perform a simple connection attempt in .net
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
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