Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ServiceStack中的IRestClient发出HEAD请求_C#_Http_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">servicestack_Httpverbs - Fatal编程技术网 servicestack,httpverbs,C#,Http,servicestack,Httpverbs" /> servicestack,httpverbs,C#,Http,servicestack,Httpverbs" />

C# 使用ServiceStack中的IRestClient发出HEAD请求

C# 使用ServiceStack中的IRestClient发出HEAD请求,c#,http,servicestack,httpverbs,C#,Http,servicestack,Httpverbs,上下文:我构建了一个REST服务来处理“Profile”对象。每个配置文件都必须具有唯一的名称。客户端出于验证目的需要执行的操作之一是检查以确保具有给定名称的概要文件不存在 与其构建一个RPC风格的“ProfileExists”方法,我更愿意遵循REST设计原则,向具有给定名称的概要文件发出HEAD请求,然后根据概要文件是否已经存在(分别为200、404)返回相应的响应代码,而不需要响应主体 按照更新的ServiceStack API的约定,我设置了一个方法来接受Head请求,并使用Fiddle

上下文:我构建了一个REST服务来处理“Profile”对象。每个配置文件都必须具有唯一的名称。客户端出于验证目的需要执行的操作之一是检查以确保具有给定名称的概要文件不存在

与其构建一个RPC风格的“ProfileExists”方法,我更愿意遵循REST设计原则,向具有给定名称的概要文件发出HEAD请求,然后根据概要文件是否已经存在(分别为200、404)返回相应的响应代码,而不需要响应主体

按照更新的ServiceStack API的约定,我设置了一个方法来接受Head请求,并使用Fiddler成功地测试了这两种情况:

public object Head(GetProfile request)
{
    ValidateRequest(request);

    HttpStatusCode responseCode;

    using (var scope = new UnitOfWorkScope())
    {
        responseCode = _profileService.ProfileExists(request.Name) ? HttpStatusCode.OK : HttpStatusCode.NotFound;

        scope.Commit();
    }

    return new HttpResult { StatusCode = responseCode };
}
问题出在客户端。事实证明,通过ServiceStack的IRestClient接口发出HEAD请求很困难。虽然有Get、Post、Put和Delete方法,但没有Head方法。在此基础上,我假设可以使用CustomMethod将HEAD动词显式指定为参数:

public bool ProfileExists(string profileName)
{
    try
    {
        var response = _restClient.CustomMethod<IHttpResult>(HttpMethods.Head, new GetProfile { Name = profileName });

        return response.StatusCode == HttpStatusCode.OK;
    }
    catch (WebServiceException ex)
    {
        if (ex.StatusCode == 404)
            return false;
    }

    // Return false for any other reason right now.
    return false;
}
if (HttpMethods.AllVerbs.Contains(httpVerb.ToUpper()))
                throw new NotSupportedException("Unknown HTTP Method is not supported: " + httpVerb);
该集合包含RFC2616及更多的所有常用动词。除非此行为是一个bug,否则对任何已知HTTP谓词引发异常表明作者对CustomMethod的意图不包括能够发出对已知HTTP谓词的请求

这就引出了我的问题:如何在ServiceStack的客户端上发出HEAD请求?

这是一个错误:

if (HttpMethods.AllVerbs.Contains(httpVerb.ToUpper()))
    throw new NotSupportedException("Unknown HTTP Method is not supported: " + httpVerb);

我刚才说的。此修复程序将于本周末发布的下一版本ServiceStack(v3.9.33+)上提供。

非常好。现在,这更有意义了。感谢您添加提交!