John Ohara
John Ohara

Reputation: 2901

Set custom headers for WebClient

I have a webclient that I want to connect with multiple vendors.

Apart from the uri and the data, there's also the headers to consider, as they can differ from vendor to vendor. Surrounding the client, I have a lot of otherstuff - so I want to write this code once.

So, I'm trying to create a base method that has all the main functionality - something like the example below - that will allow me to fill in the blanks from the calling function.

    public string Post()
    {
        try
        {
            var client = new CustomWebClient();

            return client.UploadString("", "");
        }
        catch (WebException ex)
        {
            switch (ex.Status)
            {
                case WebExceptionStatus.Timeout:
                    break;
                default:
                    break;
            }

            throw new Exception();
        }
        catch (Exception ex)
        {
            throw new Exception();
        }
        finally
        {
            client.Dispose();
        }
    }

Obviously it's easy to pass in address and data as parameters, but how can I set the headers using client.Headers.Add() or something?

I'm struggling to come up with a pattern that works and doesn't smell.

Upvotes: 0

Views: 10852

Answers (2)

Racil Hilan
Racil Hilan

Reputation: 25341

If the number of possible headers is limited, you can declare them as public enum in your CustomWebClient class and create an overload of either the constructor or the UploadString() function (whichever one you like) and pass it the value of the enum to set the header accordingly. Example:

public class CustomWebClient {
    public enum Headers { StandardForm, Json, Xml }

    public CustomWebClient() {
    }

    //This is your original UploadString.
    public string UploadString(string x, string y) {
        //Call the overload with default header.
        UploadString("...", "...", Headers.StandardForm);
    }    


    //This is the overloaded UploadString.
    public string UploadString(string x, string y, Headers header) {
       switch(header){
       case Headers.StandardForm:
           client.Headers.Add("Content-Type","application/x-www-form-urlencoded");
           break;
       case Headers.Json:
           client.Headers.Add("Content-Type","text/json");
           break;
       case Headers.Xml:
           client.Headers.Add("Content-Type","text/xml");
           break;
       }
       //Continue your code.
    }    
}

The most appealing benefits of using an enum is eliminating the possible typos and giving you intelli-sense so you don't need to remember what your options are.

Upvotes: 1

rmchndrng
rmchndrng

Reputation: 852

Since the method Post() is a public method of the CustomWebClient, it would be a good design to set all the required properties for the method post via property setters or constructor initialization.

public class CustomWebClient
{
    public NameValueCollection Headers
    {
        get;
        set;
    }

    public CustomWebClient()
    {
       this.Headers = new NameValueCollection();
    }

    //Overload the constructor based on your requirement.

    public string Post()
    {
      //Perform the post or UploadString with custom logic
    }    

    //Overload the method Post for passing various parameters like the Url(if required)
}

In the place where the CustomWebClient is being used,

using (CustomWebClient client = new CustomWebClient())
{   
     client.Headers.Add("HeaderName","Value");
     client.Post();
}

Upvotes: 2

Related Questions