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;
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Rami Sakr
  • 556
  • 1
  • 6
  • 14
  • 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? – CodeCaster Nov 27 '14 at 19:54
  • Also, `GetResponse` returns a `WebResponse` object that should be disposed of. Use `using (request.GetResponse()) {};return true;` – John Saunders Nov 27 '14 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. – Rami Sakr Nov 27 '14 at 20:10
  • 1
    @JohnSaunders this is essentially a sample code, will get to cleaning things up once I get a hold of it. – Rami Sakr Nov 27 '14 at 20:11
  • Define "is supported" - otherwise this question cannot be answered - see the first comment – BrokenGlass Nov 27 '14 at 20:17
  • I want to see if the server can fulfill my request using any of the methods I supply without sending me an HTTP Error 405 Method not allowed – Rami Sakr Nov 27 '14 at 20:19
  • It's best to start clean and stay clean. Cleaning up "later" takes time, and often, "later" never arrives. – John Saunders Nov 27 '14 at 21:08
  • But maybe the server supports GET, but doesn't support `GET /`. How would you determine that it supports `Get /index.html`? – John Saunders Nov 27 '14 at 21:08
  • The whole point is to see if the GET method is supported, if GET /index.html returns an error code, It will be probably be a 404 or a redirect, but not a 405 method not supported. – Rami Sakr Nov 27 '14 at 21:10
  • I believe that 405 can be returned when a method is not supported for a given resource. – John Saunders Nov 27 '14 at 21:14

0 Answers0