C# Silverlight只发出一个http请求
我正在开发一个Silverlight应用程序,它可以发出Http请求,从web服务器上传一个zip文件。zip文件每n:th分钟从web服务器中提取一次,这是一种由计时器控制的行为 我尝试过使用C# Silverlight只发出一个http请求,c#,silverlight,caching,browser,httpwebrequest,C#,Silverlight,Caching,Browser,Httpwebrequest,我正在开发一个Silverlight应用程序,它可以发出Http请求,从web服务器上传一个zip文件。zip文件每n:th分钟从web服务器中提取一次,这是一种由计时器控制的行为 我尝试过使用WebClient和HttpWebRequest类,结果相同。请求仅在第一次到达web服务器时才到达。第二,第三,…,发送请求并进行响应的时间。但是,请求从未到达web服务器 void _timer_Tick(object sender, EventArgs e) { tr
WebClient
和HttpWebRequest
类,结果相同。请求仅在第一次到达web服务器时才到达。第二,第三,…,发送请求并进行响应的时间。但是,请求从未到达web服务器
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);
}
这里是否存在某种浏览器缓存问题?
我希望我发出的每个请求都能一直传到web服务器。是的,浏览器可能正在缓存请求。如果要禁用该功能,可以修改服务器以发送
缓存控件:无缓存
标题,也可以向URL添加某种uniquifier,以防止浏览器缓存请求:
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;
}
}
可能是你的计时器冻结了,而不是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();