HttpWebRequest发出重复请求
本文关键字:请求 HttpWebRequest | 更新日期: 2023-09-27 18:18:08
我正在运行我的程序作为一个windows服务,我试图发送一个HTTP请求每次经过的时间(我已经设置为1分钟)。我在服务器端要做的是写一个值它从查询字符串中得到。写入文件工作,但我注意到有一些重复的值被发送?
protected override void OnStart(string[] args)
{
try
{
eventLog1.WriteEntry("In OnStart, this is another new build 016");
timer1 = new System.Timers.Timer(5000D);
timer1.AutoReset = true;
timer1.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer1.Start();
eventLog1.WriteEntry("This is after calling start method");
}
catch (Exception exxx)
{
eventLog1.WriteEntry(exxx.Data.ToString());
}
}
protected override void OnStop()
{
eventLog1.WriteEntry("In onStop.");
}
private static void timer_Elapsed(object source, ElapsedEventArgs e)
{
timer1.Stop();
el.WriteEntry("The Elapsed event was raised at " + i);
i++;// i is initialized to 0
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.example.com/Test.php?test=" + i);
request.Method = "GET";
request.Timeout = 5000;
try
{
request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);
}
catch (System.Net.WebException e1)
{
el.WriteEntry("Exception 1:" + e1.Message);
}
catch (System.Net.ProtocolViolationException e2)
{
el.WriteEntry("Exception 2:" + e2.Message);
}
catch (System.InvalidOperationException e3)
{
el.WriteEntry("Exception 3:" + e3.Message);
}
timer1.Start();
}
private static void FinishWebRequest(IAsyncResult result)
{
request.GetResponse().Close();
}
我在文件中注意到的是类似1,2,1,1,3,2,2,1,1,1的内容。我看不出我的代码有什么问题。HttpWebRequest是否有可能发送重复请求?
我认为代码是正确的。我想你在别的地方也用了I变量。尝试将i更改为其他不太常见的内容
HttpWebRequest从不发送相同的请求。
对于一个实例,让我们假设HttpRequest是重复的,那么输出应该是类似1,1,1,1,2,2,2,3.....