使用 Rest 发送具有 Stream 属性的类对象 [英] Sending a class object with Stream property using Rest

查看:59
本文介绍了使用 Rest 发送具有 Stream 属性的类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以发送一个简单的类对象,其中包含如下一些属性:

I can send a simple class object with some properties like this:

[DataContract]
public class DeviceInfo
{
    [DataMember]
    public string DeviceCode { get; set; }

    [DataMember]
    public string ClientKey { get; set; }

    [DataMember]
    [XmlIgnore]
    public DateTime Timestamp { get; set; }

    [DataMember]
    public string Token { get; set; }
}  

WindowsForm 代码:​​

WindowsForm code:

string baseAddress = "http://some_webservice_url";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseAddress);
request.Method = "POST";
request.ContentType = "application/json";

DeviceInfo customData = new DeviceInfo();
customData.DeviceCode = "123456";
customData.ClientKey = "hfhf8djf89dfk9";
customData.Timestamp = "12-12-2013";
customData.Token = "444sdf54df544r";

byte[] byteArray = Encoding.UTF8.GetBytes(Serialize<DeviceInfo>(customData));
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    MessageBox.Show(Utility.ReadResponse(response));
}  

// SERIALIZATION PART  

public static string Serialize<T>(T obj)
{
    System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());
    MemoryStream ms = new MemoryStream();
    serializer.WriteObject(ms, obj);
    string retVal = Encoding.Default.GetString(ms.ToArray());
    ms.Dispose();

    return retVal;
}

public static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        if (input != null)
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
        }
        return ms.ToArray();
    }
}  

它所做的是将 DeviceInfo 发送到服务器.
但是如何使用带有 Stream 属性 的类对象来做到这一点:

What it does is sending DeviceInfo to the server.
But how could I do this using a class object with Stream property:

public class PhotoData
{
    public string ProductId { get; set; }
    public string PhotoId { get; set; }
    public System.IO.Stream FileData { get; set; }
}  

这是我到目前为止所做的,它抛出了一个错误,说

This is what I have done so far and it is throwing an error, saying

    Type 'System.IO.FileStream' with data contract name 'FileStream:http://schemas.datacontract.org/2004/07/System.IO' 
is not expected. Consider using a DataContractResolver or add any types not known statically to the list of 
known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types 
passed to DataContractSerializer.  

StreamReader sr = new StreamReader(openFileDialog1.FileName);
byte[] fileStream = ReadFully(sr.BaseStream);

PhotoData pd = new PhotoData();
pd.FileData = sr.BaseStream;
pd.ProductId = "121513542454545";
pd.PhotoId = textBox4.Text;

baseAddress = "http://someurl/rest/UploadPhotoStream";
request = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
request.Method = "POST";
request.ContentType = "application/octet-stream";

byteArray = Encoding.UTF8.GetBytes(Serialize<PhotoData>(pd));
request.ContentLength = byteArray.Length;
Stream dataStream2 = request.GetRequestStream();
dataStream2.Write(byteArray, 0, byteArray.Length);
dataStream2.Close();

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    MessageBox.Show(Utility.ReadResponse(response));
}

推荐答案

我建议使用 byte[] 而不是 Stream.

I recommend using a byte[] instead of a Stream.

public class PhotoData
{
    public string ProductId { get; set; }
    public string PhotoId { get; set; }
    public byte[] FileData { get; set; }
}

然后您可以以常规方式使用 MemoryStream 读取/写入 FileData.我不完全确定你的方法试图做什么,但我认为它最终看起来有点像这样:

Then you can read/write to the FileData using a MemoryStream in the conventional way. I'm not entirely sure what your method is attempting to do, but I think it would end up looking a bit like this:

byte[] fileStream = ReadFully(sr.BaseStream);

PhotoData pd = new PhotoData();
pd.FileData = fileStream;

这篇关于使用 Rest 发送具有 Stream 属性的类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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