Created
February 26, 2011 08:41
-
-
Save henrik-ch/845057 to your computer and use it in GitHub Desktop.
web access to test at work
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; | |
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()); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
got it working, will be useful example.