This repository was archived by the owner on Feb 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathHttpWrapper.cs
More file actions
48 lines (45 loc) · 1.43 KB
/
HttpWrapper.cs
File metadata and controls
48 lines (45 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Net;
namespace ThoughtWorks.CruiseControl.Core.Util
{
/// <summary>
/// Used to wrap calls to HttpWebRequest.
/// </summary>
public class HttpWrapper
{
/// <summary>
/// Gets the last modified time for.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="previousModifiedTime">The previous modified time.</param>
/// <returns></returns>
/// <remarks></remarks>
public virtual DateTime GetLastModifiedTimeFor(Uri url, DateTime previousModifiedTime)
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.ProtocolVersion = HttpVersion.Version11;
request.IfModifiedSince = previousModifiedTime;
request.Credentials = CredentialCache.DefaultCredentials;
try
{
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
if (response.Headers["Last-Modified"] == null) return previousModifiedTime;
return response.LastModified;
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
// Get HttpWebResponse so that you can check the HTTP status code.
// If we get a not modified back then it was still successful.
HttpWebResponse httpResponse = (HttpWebResponse) ex.Response;
if (httpResponse.StatusCode == HttpStatusCode.NotModified)
return previousModifiedTime;
}
throw;
}
}
}
}