RESTful服务回报 - 有一个错误检查[]类型System.Byte对象的起始单元。遇到意外的字符“Y” [英] Restful service returns - There was an error checking start element of object of type System.Byte[]. Encountered unexpected character 'ÿ'

查看:192
本文介绍了RESTful服务回报 - 有一个错误检查[]类型System.Byte对象的起始单元。遇到意外的字符“Y”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想从客户端发送图像的服务。

服务定义如下所示(实施是没有任何幻想,它并没有达到有这么不包括code):

  [OperationContract的]
[WebInvoke(方法=POST,UriTemplate =图像)
流图像(byte []的图像);
 

调用客户端看起来是这样的:

 公共静态的byte [] ImageToByte(图片IMG)
{
    ImageConverter转换器=新ImageConverter();
    返程(字节[])converter.ConvertTo(IMG的typeof(字节[]));
}

字符串URI =HTTP://本地主机:8000 / RestfulService /图片;

位图IMG =新位图(Random.jpg);
字节[]的字节数组= ImageToByte(IMG);

VAR请求= WebRequest.Create(URI)为HttpWebRequest的;

如果(请求!= NULL)
{
    request.ContentType =应用/ JSON;
    request.Method =POST;
}

如果(request.Method ==POST)
{
    request.ContentLength = byteArray.Length;
    流postStream = request.GetRequestStream();
    postStream.Write(字节数组,0,byteArray.Length);
    postStream.Close();
}

如果(请求!= NULL)
{
    变种响应= request.GetResponse()作为HttpWebResponse;
 

它在与最后一行抛出一个异常

  

远程服务器返回错误:(400)错误的请求。

我已经尝试了所有可能的事情,我能想到的:

  

request.ContentType

解决方案

当我做类似的东西(发布一个原始文件)我接受输入作为流(而不是字节[])。

此外,我希望请求的内容类型是应用程序/八位字节流虽然应用/ JSON的;字符集= UTF-8。可能会工作

当我在测试这一点,使用流作为输入参数类型的WebInvoke方法和返回值的伎俩来获取请求/响应的原始体。

So I am trying to send an image from the client side to the service.

Service definition looks like this (implementation is nothing fancy and It doesn't reach there so excluding that code):

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Image")]
Stream Image(byte [] Image);

The calling client looks like this:

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;

It throws an exception at the last line with

The remote server returned an error: (400) Bad Request.

I've tried all possible things I can think of for:

request.ContentType

解决方案

Where I am doing something similar (posting a raw file) I accept the input as a Stream (not byte[]).

Also I would expect the content type of the request to be application/octet-stream although "application/json; charset=utf-8" would probably work.

When I was testing this, using Stream as the input parameter type or return value of the WebInvoke method was the trick to get the raw body of the request / response.

这篇关于RESTful服务回报 - 有一个错误检查[]类型System.Byte对象的起始单元。遇到意外的字符“Y”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆