[英]RestSharp post image to WCF REST service
I'm having some trouble uploading images to my server via RestSharp. 我在通过RestSharp将图像上传到我的服务器时遇到了一些麻烦。
I have a Rest Wcf service that accepts a Stream. 我有一个接受Stream的Rest Wcf服务。 If I use the code below I always get this Exception:
如果我使用下面的代码,我总是得到这个例外:
ProtocolViolationException Bytes to be written to the stream exceed the Content-Length bytes size specified.
ProtocolViolationException要写入流的字节超过指定的Content-Length字节大小。
What settings to I need to configure... setting the content-length header seems to make no difference. 我需要配置什么设置...设置内容长度标题似乎没有区别。
The server side doesn't receive an image, but some smaller stream of bytes. 服务器端不接收图像,而是接收较小的字节流。
Any help appreciated. 任何帮助赞赏。
Client (test) Code: 客户(测试)代码:
byte[] byteArray = File.ReadAllBytes("small.jpg");
request.AddHeader("Content-Length", int.MaxValue.ToString());//doesn't matter what length I put here
request.AddFile("image/jpeg", (requestStream) =>
{
using (var ms = new MemoryStream(byteArray))
{
ms.CopyTo(requestStream, byteArray.Length);//doesn't matter whether I add second param or not
ms.Flush();
ms.Close();
}
},
"sample",
"image/jpeg");
request.Method = Method.POST;
client.ExecuteAsync(request, (response, a) =>
{
Assert.IsNotNull(response.Content);
string content = response.Content;
resetEvent.Set();
});
Service Code (returns the Url of the stored image) 服务代码(返回存储图像的URL)
[OperationContract]
[WebInvoke(UriTemplate = "upload/{fileName}/{fileExtension}/{id}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
Message UploadPhoto(string fileName, String fileExtension, string id, Stream fileContents);
Content-Length标头是根据请求主体的内容自动设置的,因此您无需明确设置它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.