代码之家  ›  专栏  ›  技术社区  ›  Cory

如何获得有关此REST服务请求不起作用的更多详细信息?

  •  0
  • Cory  · 技术社区  · 14 年前

    我的目标是创建一个REST服务,允许我发布和获取图像。我已经得到部分工作,但我有困难张贴一个图像到我创建的服务。使用 WCF REST Service Template 40 (CS) 模板我创建了以下方法来处理我的帖子。

    [WebInvoke(Method = "POST", UriTemplate = "upload/?fileName={fileName}")]
    public void AddImageToStore(Stream file, string fileName)
    {
    // Implementation goes here.  For now method is empty.
    }
    

    下面是我用来使用上述方法的代码。如果我取消对第3行的注释,并且在正文中不发送任何内容,那么一切都正常。如果我取消注释第4行并在正文中发送文本,则不会出错。但是,如果我在流中发送图像,我会收到一个错误“BadRequest(400)不是下列之一:OK(200)…”

    Stream theStream = new MemoryStream(File.ReadAllBytes(@"C:\test.jpg"));
    Uri uri = new Uri("http://127.0.0.1:50001/MediaService/upload/?fileName=test.jpg");
    HttpClient client = new HttpClient(uri);
    // HttpContent content = HttpContent.CreateEmpty(); //Status successful
    // HttpContent content = HttpContent.Create("test", Encoding.UTF8); //Status successful
    // HttpContent content = HttpContent.Create(theStream, "image/jpeg", theStream.Length); //Error when I send a stream!
    
    HttpResponseMessage response = client.Post(uri, content);
    response.EnsureStatusIsSuccessful();
    

    这是我第一次尝试使用REST服务。我现在正纠结于如何调试这个。我怎样才能更详细地了解真正的错误是什么?“BadRequest(400)…”错误消息不是很有用。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Cory    14 年前

    经过反复试验,我终于能回答我自己的问题了。

    原来我发送的文件比默认缓冲池所能接受的要大。我最终调整了我的web.config文件对于 最大缓冲池大小 , 水位标记 ,和 最大接收消息大小