【发布时间】:2010-09-15 19:24:16
【问题描述】:
我的目标是创建一个允许我发布和获取图像的 REST 服务。我已经让 GET 部分正常工作,但是我很难将图像发布到我创建的服务中。使用 WCF REST Service Template 40 (CS) 模板,我创建了以下方法来处理我的 POST。
[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)...”错误消息不是很有帮助。
【问题讨论】:
标签: .net debugging visual-studio-2010 rest httpclient