我的目标是创建一个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)…”错误消息不是很有用。