1

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.

4

2 に答える 2

2

はい、ブラウザがリクエストをキャッシュしている可能性があります。これを無効にしたい場合は、サーバーを変更してCache-Control: no-cacheヘッダーを送信するか、URL にある種の一意識別子を追加して、ブラウザがリクエストをキャッシュしないようにすることができます。

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;
    }
}
于 2013-02-18T22:16:27.420 に答える
0

Web リクエストではなく、タイマーがフリーズする可能性があります。タイマー イベントに を入れて、Debug.WriteLine複数回呼び出されるようにします。

バックグラウンド タスクにタイマーを使用することもお勧めできません。タイマーの代わりに、リクエスト間でスリープするバックグラウンド タスクを作成することをお勧めします。このようにして、サーバー要求が長すぎても、呼び出しが重複することはありません。

次の行で何かを試してください。

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();
于 2013-02-18T22:18:57.060 に答える