测试支持的HTTP方法

时间:2014-11-27 19:52:15

标签: c# http httpwebrequest http-method http-protocols

我试图在HTTP中测试支持的方法,基本上我用动词创建httpwebrequest以查看它是否支持,如何实现这一点,我该如何实现?下面是我的代码尝试。

我的代码到目前为止

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;
}

0 个答案:

没有答案