我可以发送一个简单的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; }
}
WindowsForm代码:
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));
}
答案 0 :(得分:1)
我建议使用byte[]
代替Stream
。
public class PhotoData
{
public string ProductId { get; set; }
public string PhotoId { get; set; }
public byte[] FileData { get; set; }
}
然后,您可以使用FileData
以常规方式读取/写入MemoryStream
。我不完全确定你的方法试图做什么,但我认为它看起来有点像这样:
byte[] fileStream = ReadFully(sr.BaseStream);
PhotoData pd = new PhotoData();
pd.FileData = fileStream;