Created
May 14, 2013 15:44
-
-
Save eralston/5576983 to your computer and use it in GitHub Desktop.
A simple static class in C# holding extensions for the System.Net.HttpWebRequest class, to make life a little easier when communicating with HTTP interfaces on the web, especially JSON-based REST-style services.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
// Customize this namespace as needed | |
namespace Utility | |
{ | |
/// <summary> | |
/// Extensions methods for the HttpWebRequest class to make life a little easier | |
/// </summary> | |
public static class HttpWebRequestExtensions | |
{ | |
/// <summary> | |
/// The character encoding for the request's bytes | |
/// </summary> | |
private static readonly Encoding _Encode = System.Text.Encoding.GetEncoding("utf-8"); | |
/// <summary> | |
/// Sets the data for the given request (set the GetRequestStream stream) | |
/// NOTE: If the data is null or empty, it will not be set | |
/// </summary> | |
/// <param name="request"></param> | |
/// <param name="data"></param> | |
public static void SetRequestData(this HttpWebRequest request, string data) | |
{ | |
if (string.IsNullOrEmpty(data)) | |
return; | |
// Create a byte array of the data we want to send | |
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data); | |
// Set the content length in the request headers | |
request.ContentLength = byteData.Length; | |
// Write data | |
using (Stream requestStream = request.GetRequestStream()) | |
{ | |
requestStream.Write(byteData, 0, byteData.Length); | |
} | |
} | |
/// <summary> | |
/// Gets the response of this request as a simple string | |
/// </summary> | |
/// <param name="request"></param> | |
/// <returns></returns> | |
public static string GetResponseString(this HttpWebRequest request) | |
{ | |
// Read the string back as a string, performing all necessary disposal automatically | |
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) | |
{ | |
using (Stream responseStream = response.GetResponseStream()) | |
{ | |
using (StreamReader readStream = new StreamReader(responseStream, _Encode)) | |
{ | |
return readStream.ReadToEnd(); | |
} | |
} | |
} | |
} | |
/// <summary> | |
/// Loads the request with data and gets the response as a string as one action | |
/// </summary> | |
/// <param name="request"></param> | |
/// <param name="data"></param> | |
/// <returns></returns> | |
public static string SetRequestDataAndGetResponseString(this HttpWebRequest request, string data = null) | |
{ | |
request.SetRequestData(data); | |
return request.GetResponseString(); | |
} | |
/// <summary> | |
/// Creates a new request object to the given address using the given verb, setting reasonable defaults for the request | |
/// Just set your headers and you're ready to request | |
/// </summary> | |
/// <param name="address"></param> | |
/// <param name="verb"></param> | |
/// <returns></returns> | |
public static HttpWebRequest Create(string address, string verb) | |
{ | |
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(address); | |
request.Method = verb; | |
request.SendChunked = false; | |
request.AllowWriteStreamBuffering = true; | |
request.AllowAutoRedirect = true; | |
return request; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment