1

I'm trying to test supported methods in HTTP, essentially I'm creating httpwebrequest with verb to see if It's supported, how can I achieve this, how can I achieve that? Below is my code attempt.

My code is so far as the following

public enum enumHttpVerbs
    {
        DELETE,
        GET,
        HEAD,
        OPTIONS,
        PATCH,
        POST,
        PUT,
        TRACE
    }

public bool IsSupportedVerb(Uri url, enumHttpVerbs verb)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    switch (verb)
    {
        case enumHttpVerbs.DELETE:
            request.Method = "DELETE";
            break;
        case enumHttpVerbs.GET:
            request.Method = "GET";
            break;
        case enumHttpVerbs.HEAD:
            request.Method = "HEAD";
            break;
        case enumHttpVerbs.OPTIONS:
            request.Method = "OPTIONS";
            break;
        case enumHttpVerbs.PATCH:
            request.Method = "PATCH";
            break;
        case enumHttpVerbs.POST:
            request.Method = "POST";
            break;
        case enumHttpVerbs.PUT:
            request.Method = "PUT";
            break;
        default:
            break;
    }
    try
    {
        request.GetResponse();
        return true;
    }
    catch(Exception ex) {

    }
    return false;
}
10
  • 3
    Why do you want to do this? What will you do with the result? What is your definition of "supported"? Do you understand a request giving a non-successful response does not necessarily mean the method isn't supported, but can also mean the rest of the request wasn't understood or even rejected? Did you consider executing an OPTIONS request and checking the Allow response header? Commented Nov 27, 2014 at 19:54
  • Also, GetResponse returns a WebResponse object that should be disposed of. Use using (request.GetResponse()) {};return true; Commented Nov 27, 2014 at 20:08
  • 1
    I want to find the supported verbs, part a tool I'm creating, using the OPTIONS method might not be supported so I'll have to fallback to testing each method individually, I do understand that getting the response doesn't necessarily means that a method is supported/not. That's the whole point of the question. Commented Nov 27, 2014 at 20:10
  • 1
    @JohnSaunders this is essentially a sample code, will get to cleaning things up once I get a hold of it. Commented Nov 27, 2014 at 20:11
  • Define "is supported" - otherwise this question cannot be answered - see the first comment Commented Nov 27, 2014 at 20:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.