我正在开发一个应用程序,用于为Google + 1,Facebook共享和Tweets刮脸。我有一个请求方法接受一个URL,然后关闭并获取每个社交媒体类型的计数。请求方法和线程问题?
它的逻辑如下: -
- 采取URL
- 不要通过本地/默认的IP请求,直到我们得到速率限制/非500响应
- 在错误
- 呼叫
SelectNewProxy()
它遍历代理列表并返回一个随机尝试(避免IP请求限制的好方法) - 从列表中删除坏代理到av OID再次选择它
- 开始其每递增第二
- 呼叫
- 当计时器== 600(10分钟)
- 创建新
WebProxy
并尝试背后我们的本地/默认IP请求再次 定时器
- 复位定时器
- 创建新
冲洗和重复
的代码如下:
public string Request(string action)
{
HttpWebRequest req;
OnStatusChange(new MyArgs() { Message = "Status: Requesting..." });
string response = string.Empty;
while (response.Equals(string.Empty) && proxy != null)
{
try
{
req = (HttpWebRequest)WebRequest.Create(action);
req.Proxy = proxy;
HandleUIMessages(action, proxy);
response = new StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd();
}
catch
{
//OnProxyChange(new MyArgs() { ProxyMessage = string.Format("Proxy: {0}", proxy.Address.ToString()) });
RemoveProxy(proxy);
if (!timer.Enabled)
{
timer.Interval = (int)TimeInterval.OneSecond;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Start();
}
WebProxy reset = new SelectNewProxy();
proxy = counter >= 600 ? reset : proxy = SelectNewProxy();
}
}
return response;
}
值得一提的是,我使用ThreadPool
并且每个请求在它自己的线程中运行。它似乎可以工作,但我没有得到预期的效果,计数器达到'600'并设置为proxy = reset
,但它似乎只做了非常简短的操作,可能仅适用于第一个线程会触发它?然后调用timer_Elapsed
,并重置counter
。难道是一个线程击中它,分配proxy = reset
,然后因为counter
现在已被重置(不再> = 600),所有后续排队的线程调用SelectNewProxy()
?感觉像我漫不经心,但希望有人可以理解我想说的话,如果我猜对了,我怎么能确保所有线程获得proxy = reset
并重试我们的初始IP?
任何帮助非常感谢!
三江源
您不详细说明计数器更新的位置;可能很难有人找出为什么柜台不再> = 600没有... – 2012-07-12 14:46:17