Skip to content

Instantly share code, notes, and snippets.

@henrik-ch
Created February 26, 2011 08:41
Show Gist options
  • Save henrik-ch/845057 to your computer and use it in GitHub Desktop.
Save henrik-ch/845057 to your computer and use it in GitHub Desktop.
web access to test at work
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace RestTestApp
{
internal class Program
{
private static void Main(string[] args)
{
// Create a new request to the mentioned URL.
HttpWebRequest myWebRequest = (HttpWebRequest) WebRequest.Create("http://developer.yahoo.com/");
// Obtain the 'Proxy' of the Default browser.
IWebProxy proxy = myWebRequest.Proxy;
// Print the Proxy Url to the console.
if (proxy != null)
{
Console.WriteLine("Proxy: {0}", proxy.GetProxy(myWebRequest.RequestUri));
}
else
{
Console.WriteLine("Proxy is null; no proxy will be used");
}
WebProxy myProxy = new WebProxy();
Console.WriteLine("\nPlease enter the new Proxy Address that is to be set:");
Console.WriteLine("(Example:http://myproxy.example.com:port)");
string proxyAddress;
proxyAddress = Console.ReadLine();
if (proxyAddress.Length > 0)
{
Console.WriteLine("\nPlease enter the Credentials (may not be needed)");
Console.WriteLine("Username:");
string username;
username = Console.ReadLine();
Console.WriteLine("\nPassword:");
string password;
password = Console.ReadLine();
// Create a new Uri object.
Uri newUri = new Uri(proxyAddress);
// Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
myProxy.Address = newUri;
// Create a NetworkCredential object and associate it with the
// Proxy property of request object.
myProxy.Credentials = new NetworkCredential(username, password);
myWebRequest.Proxy = myProxy;
}
Console.WriteLine("\nThe Address of the new Proxy settings are {0}", myProxy.Address);
using (HttpWebResponse response = myWebRequest.GetResponse() as HttpWebResponse)
{
//get response stream
if (response != null)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
//console app output
Console.WriteLine(reader.ReadToEnd());
}
}
}
}
}
@henrik-ch
Copy link
Author

got it working, will be useful example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment