I wrote a small app that downloads YouTube videos but like most company's they don't allow you to view YouTube so i would like to incorporate a proxy option into my app to download the YouTube videos through a supplied proxy ip and port.
You are probably looking for this configuration settings:
<configuration>
<system.net>
<defaultProxy>
<proxy
usesystemdefault="true"
proxyaddress="http://192.168.1.10:3128"
bypassonlocal="true"
/>
</defaultProxy>
</system.net>
</configuration>
This way you can reference the system proxy settings or override them with your own.
If your app derives from WebRequest you could look into WebRequest.Proxy.
UPDATE
To make your .NET app use a specific proxy do the following:
Create a file named [Your_App_Name].exe.config in the same folder your app resides
Edit the newly created file to contain the following:
<?xml version="1.0">
<configuration>
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"
proxyaddress="<YOUR_PROXY_IP_ADDRESS>:<YOUR_PROXY_PORT>"
bypassonlocal="True" />
</defaultProxy>
</system.net>
</configuration>
Replace <YOUR_PROXY_IP_ADDRESS> and <YOUR_PROXY_PORT> with the proxy you'd like to use.
This is the proxy class... if this helps.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Windows.Forms; using System.Net;
namespace yourtube {
public class Proxy { public static string g_proxyDomain="", g_proxyServer="", g_proxyPassword="", g_proxyUser=""; public static int g_proxyPort; public static bool g_UseProxy; private static System.Net.WebProxy g_WebProxy;
/// <summary>
/// Returns a new proxy object according to the proxy variables.
/// </summary>
/// <returns></returns>
public static System.Net.WebProxy GetProxy()
{
if (g_WebProxy == null)
{
try
{
System.Net.WebProxy myProxy = new System.Net.WebProxy(g_proxyServer, g_proxyPort);
myProxy.Credentials = new System.Net.NetworkCredential(g_proxyUser, g_proxyPassword, g_proxyDomain);
myProxy.BypassProxyOnLocal = true;
g_WebProxy = myProxy;
WebRequest.DefaultWebProxy = myProxy;
}
catch
{
//g_WebProxy=System.Net.GlobalProxySelection.GetEmptyWebProxy();
}
}
return g_WebProxy;
}
public static void SetDefaultConfig()
{
Proxy.g_UseProxy = false;
Proxy.g_proxyServer = "10.0.0.1";
Proxy.g_proxyPort = 8080;
Proxy.g_proxyDomain = "";
Proxy.g_proxyPassword = "";
Proxy.g_proxyUser = "";
}
public static bool WriteOptions()
{
System.IO.FileStream myWriter = null;
string[] vals = new string[15];
try
{
//Not using the config parameters for the URL
//Proxy.g_GMServerURL = @"https://mail.google.com/mail/feed/atom";
//IMP: Do not Change Sequence of the Parameters
vals[0] = Convert.ToString(Proxy.g_UseProxy);
vals[1] = (Proxy.g_proxyServer == null) ? "" : Proxy.g_proxyServer;
vals[2] = Convert.ToString(Proxy.g_proxyPort);
vals[3] = Proxy.g_proxyUser.Trim();
vals[4] = Proxy.g_proxyPassword.Trim();
vals[5] = Proxy.g_proxyDomain.Trim();
myWriter = System.IO.File.OpenWrite(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\vals.conf");
byte[] bytdata;
string data = "";
int i;
for (i = 0; i < 15; i++)
{
data += vals[i] + Convert.ToString(Convert.ToChar(13));
}
data = data.Substring(0, data.Length - 2);
bytdata = System.Text.Encoding.UTF8.GetBytes(data);
myWriter.Write(bytdata, 0, bytdata.Length);
myWriter.Flush();
return true;
}
catch (Exception ex)
{
string strErr = ex.Message;
MessageBox.Show("Errors Occured while writing configuration ", "YourTube", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
finally
{
if (!(myWriter == null))
{
myWriter.Close();
}
}
}
public static bool ReadOptions()
{
System.IO.FileStream reader = null;
try
{
//Create a file if it does not exist. Create it with the default parameters.
if (!System.IO.File.Exists(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\vals.conf"))
{
WriteOptions();
}
}
catch
{ }
try
{
reader = System.IO.File.OpenRead(System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\vals.conf");
string readData;
byte[] bytData = null;
string[] vals = new string[15];
bytData = new byte[reader.Length];
reader.Read(bytData, 0, bytData.Length);
readData = System.Text.Encoding.UTF8.GetString(bytData);
vals = readData.Split(Convert.ToChar(13));
//IMP: Do not Change Sequence of the Parameters
Proxy.g_UseProxy = Convert.ToBoolean(vals[0]);
Proxy.g_proxyServer = vals[1];
Proxy.g_proxyPort = Convert.ToInt32(vals[2]);
Proxy.g_proxyUser = vals[3];
Proxy.g_proxyPassword = vals[4];
Proxy.g_proxyDomain = vals[5];
//Not using the config parameters for the URL
//Proxy.g_GMServerURL = @"https://mail.google.com/mail/feed/atom";
return true;
}
catch (Exception ex)
{
string strErr = ex.Message;
MessageBox.Show("Errors Occured while reading configuration. Some configuration will be changed ", "GMReader", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (!(reader == null))
{
reader.Close();
}
WriteOptions();
return false;
}
finally
{
if (!(reader == null))
{
reader.Close();
}
}
}
}
}