Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / Windows Forms

An Editor for Communicating with RESTful Services

Rate me:
Please Sign up or sign in to vote.
3.75/5 (3 votes)
3 Mar 2009Public Domain3 min read 28.7K   574   27  
A Windows application (.NET) which allows the user to GET, PUT, POST, or DELETE data to a URI
//-----------------------------------------------------------------------
// <copyright file="Extensions.cs" company="None">
//     No copyright restrictions apply.
// </copyright>
//-----------------------------------------------------------------------
namespace RestPad
{
    using System;
    using System.ComponentModel;
    using System.Net;

    /// <summary>
    /// The module which holds all extension methods used by RestPad.
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// Gets the value of the specified header, or returns the defaultValue if no header exists.
        /// </summary>
        /// <typeparam name="T">The type of value to retrieve.</typeparam>
        /// <param name="headers">The headers collection.</param>
        /// <param name="key">The header key.</param>
        /// <param name="defaultValue">The default value to be returned if the header does not exist.</param>
        /// <returns>The value of the header or defaultValue if the header cannot be found.</returns>
        public static T GetValue<T>(this WebHeaderCollection headers, string key, T defaultValue)
        {
            string header = headers[key];
            if (string.IsNullOrEmpty(header))
            {
                return defaultValue;
            }

            return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(header);
        }

        /// <summary>
        /// Gets a value indicating whether or not the exception is dangerous.
        /// </summary>
        /// <param name="ex">The exception to evaluate.</param>
        /// <returns>A value indicating whether or not the exception is dangerous.</returns>
        public static bool IsDangerous(this Exception ex)
        {
            return ex is OutOfMemoryException;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer
United States United States
I'm a professional geek.

Comments and Discussions