问题描述
我在脚本中使用来自 https://github.com/SirCmpwn/RedditSharp 的 RedditSharp我的,我只是问,使用它连接时如何实现代理?我可以更改代理中标吗?
I'm using RedditSharp from https://github.com/SirCmpwn/RedditSharp in a script of mine, and I'm simply asking, when connecting using this how do I implement a proxy? and could I change the proxy midscript?
推荐答案
没有独立的方法,不修改这个库源代码是无法完成的.
There no standalone way, you can't accomplish this without modifying this library source code.
最无痛的方式:
RedditSharp 的重载构造函数 - 添加以 IWebAgent 作为类型的新参数.所以它看起来像这样:
Overload constructor of RedditSharp - add new argument with IWebAgent as type. So it will look like this:
public Reddit() : this(new WebAgent())
{
}
public Reddit(IWebAgent agent)
{
JsonSerializerSettings = new JsonSerializerSettings();
JsonSerializerSettings.CheckAdditionalContent = false;
JsonSerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
_webAgent = agent;
CaptchaSolver = new ConsoleCaptchaSolver();
}
从 RedditSharp.WebAgent 类声明中删除密封"关键字.
Remove "sealed" keyword from RedditSharp.WebAgent class declaration.
使 RedditSharp.WebAgent.CreateRequest 方法成为虚拟方法,所以它看起来像这样:
Make RedditSharp.WebAgent.CreateRequest method virtual, so it will look like this:
public virtual HttpWebRequest CreateRequest(string url, string method, bool prependDomain = true)
{
...
}
基于旧版创建您自己的 WebAgent:
Create your own WebAgent based on old-one:
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;
}
}
在您的代码中使用它:
Use it in your code:
var agent = new MyAgent();
var reddit = new Reddit(agent);
...
agent.Proxy = new WebProxy("someproxy.net", 8080);
所以现在您可以随时随地设置代理.没有真正测试过,但它必须工作.
So now you can set proxy anytime, from anywhere. Not tested really, but it must work.
这篇关于如何在 RedditSharp 中使用代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!