Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用每个控制台应用程序的代理_C#_Proxy_Console Application - Fatal编程技术网

C# 使用每个控制台应用程序的代理

C# 使用每个控制台应用程序的代理,c#,proxy,console-application,C#,Proxy,Console Application,我有一个VM,它运行一些web爬行控制台应用程序。我想知道如何为每个控制台应用程序使用不同的代理。我发现的大多数代理C#功能都会更改注册表,但这会更改所有控制台应用程序,而不仅仅是一个 有没有人举过一个例子,说明如何在不影响所有其他控制台应用的情况下为特定控制台应用使用特定代理 寻找源代码解决方案假设您的控制台应用程序使用一种内置的下载网页方法(WebClient、HttpWebRequest等),它们都有一个可以满足您需要的功能。它们的工作原理基本相同,下面是MSDN文档为HttpWebReq

我有一个VM,它运行一些web爬行控制台应用程序。我想知道如何为每个控制台应用程序使用不同的代理。我发现的大多数代理C#功能都会更改注册表,但这会更改所有控制台应用程序,而不仅仅是一个

有没有人举过一个例子,说明如何在不影响所有其他控制台应用的情况下为特定控制台应用使用特定代理


寻找源代码解决方案

假设您的控制台应用程序使用一种内置的下载网页方法(WebClient、HttpWebRequest等),它们都有一个可以满足您需要的功能。它们的工作原理基本相同,下面是MSDN文档为HttpWebRequest.Proxy提供的示例:

// Create a new request to the mentioned URL.               
HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.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;

try
{
    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);
    HttpWebResponse myWebResponse=(HttpWebResponse)myWebRequest.GetResponse();

您可以为每个控制台应用程序创建自己的代理身份验证类

namespace YourProxyNameSpace
{
  public class YourProxyClass: IWebProxy
  {
        public Uri GetProxy(Uri destination)
        {
            string proxy = ConfigurationManager.AppSettings["proxyaddress"];
            return new Uri(proxy);
        }

        public bool IsBypassed(Uri host)
        {
            return false;
        }

        public ICredentials Credentials
        {
            get
            {
                string username = ConfigurationManager.AppSettings["username"];
                string password = ConfigurationManager.AppSettings["password"];
                return new NetworkCredential(username, password);
            }
            set { }
        }
    }
}
在配置文件(app.config)中添加以下节点



希望它能帮助别人。:)

您是在寻找一种方法来更改某个控制台应用程序的源代码,还是在寻找一种外部方法来控制您没有源代码的程序所使用的代理?
<system.net>    
<defaultProxy>
<module type="YourProxyNameSpace.YourProxyClass, YourProxyNameSpace"/>    
</defaultProxy>
</system.net>


 <add key="proxyaddress" value="http://proxyAddress:PORT"/>
    <add key="username" value="*****"/>
    <add key="password" value="*****"/>