我需要记录对文件的裸 protobuf 响应并将其反序列化为对象。它只是让我做其中之一。下面的代码会生成正确反序列化的对象,但也会生成空白文本文件。我怎样才能做到这一点?
try
{
ProtoBuf.Serializer.Serialize(webRequest.GetRequestStream(), myclass);
}
finally
{
webRequest.GetRequestStream().Close();
}
var webRequest = (HttpWebRequest)WebRequest.Create(EndPoint);
webRequest.Method = "POST";
WebResponse response = webRequest.GetResponse();
var responseStream = response.GetResponseStream();
//deserializing using the response stream
myotherclassinstance = ProtoBuf.Serializer.Deserialize<TidalTV.GoogleProtobuffer.BidResponse>(responseStream);
//trying and failing to copy the response stream to the filestream
using (var fileStream = File.Create(Directory.GetCurrentDirectory() + "\\ProtobufResponse"))
{
responseStream.CopyTo(fileStream);
}
最佳答案
您需要在反序列化和写入文件之间将流寻找回 0
如果流不可查找,则在执行其他操作之前将其复制到内存流
然后使用
stream.Seek(0, SeekOrigin.Begin);
关于c# - 如何使用 protobuf 反序列化流并将其写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21731262/