C# 如何将代理与RedditSharp一起使用?

C# 如何将代理与RedditSharp一起使用?,c#,proxy,reddit,C#,Proxy,Reddit,我在我的一个脚本中使用了RedditSharp from,我只是问,当使用这个连接时,我如何实现代理?我可以更改代理midscript吗?没有独立的方法,如果不修改此库源代码,就无法完成此操作 所以最无痛的方式是: RedditSharp的重载构造函数-添加类型为IWebAgent的新参数。所以它看起来是这样的: public Reddit() : this(new WebAgent()) { } public Reddit(IWebAgent agent) { JsonSerial

我在我的一个脚本中使用了RedditSharp from,我只是问,当使用这个连接时,我如何实现代理?我可以更改代理midscript吗?

没有独立的方法,如果不修改此库源代码,就无法完成此操作

所以最无痛的方式是:

  • RedditSharp的重载构造函数-添加类型为IWebAgent的新参数。所以它看起来是这样的:

    public Reddit() : this(new WebAgent())
    {
    
    }
    
    public Reddit(IWebAgent agent)
    {
        JsonSerializerSettings = new JsonSerializerSettings();
        JsonSerializerSettings.CheckAdditionalContent = false;
        JsonSerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
        _webAgent = agent;
        CaptchaSolver = new ConsoleCaptchaSolver();
    }
    
    public virtual HttpWebRequest CreateRequest(string url, string method, bool prependDomain = true)
    {
        ...
    }
    
  • 从RedditSharp.WebAgent类声明中删除“sealed”关键字

  • 将RedditSharp.WebAgent.CreateRequest方法设置为虚拟,使其如下所示:

    public Reddit() : this(new WebAgent())
    {
    
    }
    
    public Reddit(IWebAgent agent)
    {
        JsonSerializerSettings = new JsonSerializerSettings();
        JsonSerializerSettings.CheckAdditionalContent = false;
        JsonSerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
        _webAgent = agent;
        CaptchaSolver = new ConsoleCaptchaSolver();
    }
    
    public virtual HttpWebRequest CreateRequest(string url, string method, bool prependDomain = true)
    {
        ...
    }
    
  • 基于旧的WebAgent创建您自己的WebAgent:

    public class MyAgent: WebAgent
    {
        public IWebProxy Proxy { get; set; }
    
        public override HttpWebRequest CreateRequest(string url, string method, bool prependDomain = true)
        {
            var base_request = base.CreateRequest(url, method, prependDomain);
    
            if (Proxy != null)
            {
                base_request.Proxy=Proxy;   
            }
    
            return base_request;
        }
    }
    
  • 在代码中使用它:

    var agent = new MyAgent();
    var reddit = new Reddit(agent);
    
    ...
    
    agent.Proxy = new WebProxy("someproxy.net", 8080);
    

  • 现在,您可以随时随地设置代理。确实没有测试,但它必须工作。

    什么类型的代理?远程呼叫代理?网络代理站点?模拟对象?你能把你的问题说得更具体些吗?我打算用袜子。谢谢!然而,我做到了这一点,它似乎不适合我。当我登录时,无论代理是什么,登录都是成功的,即使它被设置为不正确的代理IP和端口。它是否被检测为不真实,然后被忽略?我如何知道它是否真的在使用代理,如何在尝试使用它之前测试代理是否正常工作?就像我说的,我如何绝对确保它实际上使用代理登录?经过一些实验,我非常确信我尝试使用的代理在代码中不起作用,并且被忽略了。因为如果我尝试使用代理多次使用错误密码登录,它仍然会阻止我登录,并说当我在FireFox中访问没有代理的网站时,我必须等待。我一定是做错了什么。