使用 restsharp 在 WCF Web api 服务上发布 http 帖子

Posted

技术标签:

【中文标题】使用 restsharp 在 WCF Web api 服务上发布 http 帖子【英文标题】:http post on a WCF web api service using restsharp 【发布时间】:2011-12-04 15:02:34 【问题描述】:

我试图在使用 WCF Web Api 编程的我的 api 中发布一些信息。在客户端中,我使用的是restsharp,它是用于restful 服务的rest 客户端。但是,当我尝试将一些参数添加到请求中时,从不调用服务中的 post 方法,并且客户端中的响应对象获得 500 状态(内部服务器错误),但是当我评论我的行时m 添加参数请求到达服务中暴露的post方法。

这是来自客户端的代码:

[HttpPost]
    public ActionResult Create(Game game)
    
        if (ModelState.IsValid)
        
            var request = new RestRequest(Method.POST);
            var restClient = new RestClient();
            restClient.BaseUrl = "http://localhost:4778";
            request.Resource = "games";
            //request.AddParameter("Name", game.Name,ParameterType.GetOrPost); this is te line when commented     everything works fine
            RestResponse<Game> g = restClient.Execute<Game>(request);
            return RedirectToAction("Details", new id=g.Data.Id );
        
        return View(game);
    

这是服务的代码:

[WebInvoke(UriTemplate = "", Method = "POST")]
    public HttpResponseMessage<Game> Post(Game game, HttpRequestMessage<Game> request)
    
        if (null == game)
        
            return new HttpResponseMessage<Game>(HttpStatusCode.BadRequest);
        
        var db = new XBoxGames();
        game = db.Games.Add(game);
        db.SaveChanges();

        HttpResponseMessage<Game> response = new HttpResponseMessage<Game>(game);
        response.StatusCode = HttpStatusCode.Created;

        var uriBuilder = new UriBuilder(request.RequestUri);
        uriBuilder.Path = string.Format("games/0", game.Id);
        response.Headers.Location = uriBuilder.Uri;
        return response;
    

我需要向我的请求添加参数,以便填充服务中的游戏对象,但我不知道该怎么做,如果每次我尝试添加参数时服务都会中断。

我忘了提到客户端和服务器都是 .NET MVC 3 应用程序。

任何帮助将不胜感激。提前致谢。

【问题讨论】:

【参考方案1】:

我注意到您将 Game 作为参数并在 HttpRequestMessage 中。你不需要这样做。一旦你有了请求(即你的请求参数),你可以在 Content 属性上调用 ReadAs,你将获得 Game 实例。您两次通过 Game 的事实可能是导致问题的原因。您可以尝试删除第二个游戏参数并在响应中使用那个吗?

WCF Web API 确实支持表单 url 编码。在 Preview 5 中,如果您使用 MapServiceRoute 扩展方法,它将自动连接。如果不是,则创建一个 WebApiConfiguration 对象并将其传递给您的 ServiceHostFactory / ServiceHost。

【讨论】:

【参考方案2】:

好吧,在一遍又一遍地解决这个问题后,我终于找到了解决方案,但是,我无法解释为什么会发生这种情况。

我将 addParameter 方法替换为 addBody,一切正常,我可以在服务器上发布信息。

问题似乎是每当我通过addParameter方法添加参数时,该方法会将参数附加为application/x-www-form-urlencoded,显然WCF web api不支持这种类型的数据,就是这样为什么它会向客户端返回内部服务器错误。

相反,addBody 方法使用服务器可以理解的 text/xml。

再说一次,我不知道这是否真的发生了,但似乎就是这样。

这就是我的客户端代码现在的样子:

[HttpPost]        
    public ActionResult Create(Game game)
    
        if (ModelState.IsValid)
        
            RestClient restClient = new RestClient("http://localhost:4778");
            RestRequest request = new RestRequest("games/daniel",Method.POST);
            request.AddBody(game);
            RestResponse response = restClient.Execute(request);
            if (response.StatusCode != System.Net.HttpStatusCode.InternalServerError)
            
                return RedirectToAction("Index");
            
        
        return View(game);

如果您有任何反馈,或者知道发生了什么,请告诉我。

【讨论】:

【参考方案3】:

我不熟悉你正在调用的对象,但 game.Name 是一个字符串吗?如果不是,这可能解释了 AddParameter 失败的原因。

【讨论】:

以上是关于使用 restsharp 在 WCF Web api 服务上发布 http 帖子的主要内容,如果未能解决你的问题,请参考以下文章