c# - 如何使用 C# 实现重试模式?
问题描述
我有一个代理服务器列表。使用这些代理服务器,我想点击 URL 以编程方式获取数据。我想在这里应用重试模式。例如,如果我无法从代理服务器 1 获取数据,我想使用代理服务器 2 访问 URL,依此类推。根据良好的设计实践,我应该在ProxyHelper 类 GetData 方法中写什么。
示例代码:
Interface IProxyServer {
IResponse GetData(String URL);
}
public class ProxyServerA implements IProxyServer {
public IResponse GetData(String URL)
{
//some code
}
}
public class ProxyServerB implements IProxyServer {
public IResponse GetData(String URL) {
//some code
}
}
public class ProxyHelper {
public List<IProxyServer> _proxyservers = new List<IProxyServer>();
public IResponse GetData(String URL)
{
// Following the best practices, what should be the code here which will implement retry mechanism
// and call other available proxy server in case the request is failed.
}
public void Add(IProxyServer proxyserver)
{
_proxyservers.Add(proxyserver);
}
}
void main() {
ProxyHelper proxyHelper = new ProxyHelper();
String URL = "www.google.com";
proxyserver.Add(new ProxyServerA());
proxyserver.Add(new ProxyServerB());
var response = ProxyHelper.GetData(URL);
}
解决方案
推荐阅读
- python - Pandas 根据值重新定位列值
- javafx - 错误“javafx.scene.CssStyleHelper calculateValue”是什么意思?
- javascript - 实现一个接受项目列表和回调的函数
- thinkscript - 如何创建一个保留其值的变量
- css - SVG将渐变修改为通过“使用”重用的元素
- laravel - Laravel Jobs:假设索引“job”导致错误
- android - 如何从 WindowManager 获取“视图”
- c++ - 使用 libharu 将 PNG 图像从文件加载到内存缓冲区
- javascript - axios 不返回承诺,不同步
- mongodb - 删除有条件的聚合