This is my first attempt at creating a console app
which can make a HTTP GET
request and print the response to the console
.
Thus far, the code works, but only for URIs
which do not require a username/password
.
My ultimate purpose is to use a cloud/hosting API
which accepts HTTP GET
requests as triggers for taking certain actions. As such, I have to use a username/password
for this.
using System;
using System.Net;
using System.IO;
namespace HttpTestProject {
class Program {
static void Main(string[] args) {
Uri uri = new Uri("http://www.google.com");
string username = "asdf";
string password = "asdf";
NetworkCredential cred = new NetworkCredential(username, password);
CredentialCache cache = new CredentialCache();
cache.Add(uri, "Basic", cred);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
string text = reader.ReadToEnd();
Console.WriteLine(text);
Console.ReadLine();
}
}
}