Permalink
Browse files

Added HttpWebRequestUtility. Closes #2

  • Loading branch information...
1 parent c8564fb commit b78b5e92e1a4dfac30b322402359a6deb51c53f3 @bgrainger bgrainger committed Feb 6, 2010
@@ -56,6 +56,7 @@
<Compile Include="HashCodeUtility.cs" />
<Compile Include="IO\WrappingStream.cs" />
<Compile Include="NativeMethods.cs" />
+ <Compile Include="Net\HttpWebRequestUtility.cs" />
<Compile Include="ObjectImpl.cs" />
<Compile Include="ObjectUtility.cs" />
<Compile Include="Ownership.cs" />
@@ -0,0 +1,36 @@
+
+using System.Net;
+
+namespace Logos.Utility.Net
+{
+ /// <summary>
+ /// Provides utility methods for working with <see cref="HttpWebRequest"/>.
+ /// </summary>
+ public static class HttpWebRequestUtility
+ {
+ /// <summary>
+ /// Gets the <see cref="HttpWebResponse"/> from an Internet resource.
+ /// </summary>
+ /// <param name="request">The request.</param>
+ /// <returns>A <see cref="HttpWebResponse"/> that contains the response from the Internet resource.</returns>
+ /// <remarks>This method does not throw a <see cref="WebException"/> for "error" HTTP status codes; the caller should
+ /// check the <see cref="HttpWebResponse.StatusCode"/> property to determine how to handle the response.
+ /// See <a href="http://code.logos.com/blog/2009/06/using_if-modified-since_in_http_requests.html">Using
+ /// If-Modified-Since in HTTP Requests</a>.</remarks>
+ public static HttpWebResponse GetHttpResponse(this HttpWebRequest request)
+ {
+ try
+ {
+ return (HttpWebResponse) request.GetResponse();
+ }
+ catch (WebException ex)
+ {
+ // only handle protocol errors that have valid responses
+ if (ex.Response == null || ex.Status != WebExceptionStatus.ProtocolError)
+ throw;
+
+ return (HttpWebResponse) ex.Response;
+ }
+ }
+ }
+}
@@ -0,0 +1,33 @@
+
+using System;
+using System.Net;
+using Logos.Utility.Net;
+using NUnit.Framework;
+
+namespace Logos.Utility.Tests
+{
+ [TestFixture]
+ public class HttpWebRequestUtilityTests
+ {
+ [Test, Ignore("Requires internet connection")]
+ public void Get404Response()
+ {
+ HttpWebRequest request = (HttpWebRequest) WebRequest.Create(new Uri("http://code.logos.com/non-existent"));
+ using (HttpWebResponse response = request.GetHttpResponse())
+ {
+ Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotFound));
+ }
+ }
+
+ [Test, Ignore("Requires internet connection")]
+ public void Get304Response()
+ {
+ HttpWebRequest request = (HttpWebRequest) WebRequest.Create(new Uri("http://code.logos.com/blog/blogCodeBackground.gif"));
+ request.IfModifiedSince = new DateTime(2009, 1, 1);
+ using (HttpWebResponse response = request.GetHttpResponse())
+ {
+ Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NotModified));
+ }
+ }
+ }
+}
@@ -58,6 +58,7 @@
<Compile Include="EquatableStructTests.cs" />
<Compile Include="ExpressionUtilityTests.cs" />
<Compile Include="HashCodeUtilityTests.cs" />
+ <Compile Include="HttpWebRequestUtilityTests.cs" />
<Compile Include="ObjectUtilityTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Salsa20Tests.cs" />

0 comments on commit b78b5e9

Please sign in to comment.