日期:2014-05-17 浏览次数:21476 次
public static void UpdateFile(string url, string filePath)
{
HttpWebResponse Response;
//Retrieve the File
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);
Request.Headers.Add("Translate: f");
Request.Credentials = CredentialCache.DefaultCredentials;
//Set up the last modfied time header
if (File.Exists(filePath))
Request.IfModifiedSince = LastModFromDisk(filePath);
try
{
Response = (HttpWebResponse)Request.GetResponse();
}
catch(WebException e)
{
if (e.Response == null)
{
Debug.WriteLine("Error accessing Url " + url);
throw;
}
HttpWebResponse errorResponse = (HttpWebResponse)e.Response;
//if the file has not been modified
if (errorResponse.StatusCode == HttpStatusCode.NotModified)
{
e.Response.Close();
return;
}
else
{
e.Response.Close();
Debug.WriteLine("Error accessing Url " + url);
throw;
}
}
Stream respStream = null;
try
{
respStream = Response.GetResponseStream();
CopyStreamToDisk(respStream,filePath);
DateTime d = System.Convert.ToDateTime(Response.GetResponseHeader("Last-Modified"));
File.SetLastWriteTime(filePath,d);
}
catch (Exception)
{
Debug.WriteLine("APPMANAGER: Error writing to: " + filePath);
throw;
}
finally
{
if (respStream != null)
respStream.Close();
if (Response != null)
Response.Close();
}
}