Sending a class object with Stream property using

2019-08-28 09:05发布

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 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();
    }
}  

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));
}

标签: c# rest upload
1条回答
Melony?
2楼-- · 2019-08-28 09:20

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; }
}

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;
查看更多
登录 后发表回答