所以我试图将图像从客户端发送到服务。
服务定义看起来像这样(实现没有什么花哨的,它没有到达那里,所以排除了该代码):
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Image")]
Stream Image(byte [] Image);
调用客户端如下所示:
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
string uri = "http://localhost:8000/RestfulService/Image";
Bitmap img = new Bitmap("Random.jpg");
byte[] byteArray = ImageToByte(img);
var request = WebRequest.Create(uri) as HttpWebRequest;
if (request != null)
{
request.ContentType = "application/json";
request.Method = "POST";
}
if (request.Method == "POST")
{
request.ContentLength = byteArray.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
}
if (request != null)
{
var response = request.GetResponse() as HttpWebResponse;
它在最后一行抛出异常
远程服务器返回错误:(400) 错误请求。
我已经尝试了我能想到的所有可能的事情:
请求内容类型