Reputation: 19
I'm working on a Silverlight app which among other things makes Http requests in which it uploads a zip file from the web server. The zip file is picked up from the web server every n:th minute, a behavior controlled by a timer.
I've tried using the WebClient
and HttpWebRequest
classes with the same result. The request only reaches the web server the first time. The second, third, ..., time the request is sent and a response will occur. However, the request never reaches the web server...
void _timer_Tick(object sender, EventArgs e)
{
try
{
HttpWebRequest req = WebRequest.CreateHttp(_serverUrl + "channel.zip");
req.Method = "GET";
req.BeginGetResponse(new AsyncCallback(WebComplete), req);
}
catch (Exception ex)
{
throw ex;
}
}
void WebComplete(IAsyncResult a)
{
HttpWebRequest req = (HttpWebRequest)a.AsyncState;
HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a);
Stream stream = res.GetResponseStream();
byte[] content = readFully(stream);
unzip(content);
}
Is there some kind of browser caching issue here? I want every request I make to go all the way to the web server.
Upvotes: 1
Views: 523
Reputation: 11040
Chances are your timer freezes, not the web request. Put a Debug.WriteLine
in your timer event, make sure it gets called more than once.
It's also a bad idea to use timers for background tasks. Instead of a timer it's a better option to create a background task that sleeps between requests. This way even too long of server request won't cause calls to overlap.
Try something in the lines of:
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork+=(s,a)=>{
try{
while (true)// or some meaningful cancellation condition is false
{
DownloadZipFile();
Sleep(FiveMinutes);
// don't update UI directly from this thread
}
} catch {
// show something to the user so they know automatic check died
}
};
worker.RunAsync();
Upvotes: 0
Reputation: 87293
Yes, the browser may be caching the request. If you want to disable that, you can either modify the server to send a Cache-Control: no-cache
header, or you can append some sort of uniquifier to the URL that will prevent the browser from caching the request:
void _timer_Tick(object sender, EventArgs e)
{
try
{
HttpWebRequest req = WebRequest.CreateHttp(_serverUrl + "channel.zip?_=" + Environment.TickCount);
req.Method = "GET";
req.BeginGetResponse(new AsyncCallback(WebComplete), req);
}
catch (Exception ex)
{
throw ex;
}
}
Upvotes: 2