0

我可以发送一个简单class object的一些属性,如下所示:

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

Windows窗体代码:

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 property

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

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

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

1 回答 1

1

我建议使用 abyte[]而不是 a Stream

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

然后您可以以传统方式读取/写入FileDatausing a MemoryStream。我不完全确定你的方法试图做什么,但我认为它最终看起来有点像这样:

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

PhotoData pd = new PhotoData();
pd.FileData = fileStream;
于 2013-03-25T05:50:37.600 回答