这个问题有点复杂,请教高手

C语言答疑课堂 2009-04-01 07:06:15
我将一个类ServiceItem
namespace ServiceItem
{
[Serializable]
public class ServiceItem
{
//服务的ID
private string serviceID;
public string ServiceID
{
get { return serviceID; }
set { serviceID = value; }
}

//服务的类型
private string type;
public string Type
{
get { return type; }
set { type = value; }
}

//服务的地址
private string address;
public string Address
{
get { return address; }
set { address = value; }
}

//租用时间
private long Time;
public long rentTime
{
get { return Time; }
set { Time = value; }
}

public ServiceItem()
{
}
public ServiceItem(string type, string address)
{
Type = type;
Address = address;
}
}
}
用BinaryFormatter序列化后以Binary文件上传到http://172.18.10.5上。现在我编写了如下程序:
namespace 下载服务代理
{
class Program
{
static void Main(string[] args)
{
WebClient client = new WebClient();

string URL = "http://172.18.10.5/Binary";
int n = URL.LastIndexOf("/");
string URLAddress = URL.Substring(0, n);
string fileName = URL.Substring(n + 1, URL.Length - n - 1);
string Dir = "d:/";
string Path = Dir + "\\\\" + fileName;

try
{
WebRequest myre = WebRequest.Create(URLAddress);
}
catch (WebException exp)
{
Console.WriteLine(exp.Message);
}

try
{
//statusBar.Text = "开始下载文件...";
client.DownloadFile(URLAddress, fileName);
Stream str = client.OpenRead(URLAddress);
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
//statusBar.Text = \"正在接收数据...\";
while (allmybyte > 0)
{
int m = str.Read(mbyte, startmbyte, allmybyte);
if (m == 0)
break;

startmbyte += m;
allmybyte -= m;
}

IFormatter formatter = new BinaryFormatter();

ServiceItem.ServiceItem service = (ServiceItem.ServiceItem)formatter.Deserialize(str);
Console.WriteLine("service.Address = {0}", service.Address);
Console.WriteLine("service.rentTime = {0}", service.rentTime);
Console.WriteLine("service.ServiceID = {0}", service.ServiceID);
Console.WriteLine("service.Type = {0}", service.Type);

str.Close();
fstr.Close();
//statusBar.Text = \"下载完毕!\";
}
catch (WebException exp)
{
Console.WriteLine(exp.Message);
//statusBar.Text = \"\";
}
}
}
}
我的意图是用这个程序从http://172.18.10.5/上下载Binary这个文件,并且把它反序列化成ServiceItem类赋给service对象,输出service对象的各个属性。可是老是不成功。麻烦哪位高手能帮我指点迷津,谢谢啦!
...全文
92 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangqidong 2009-04-01
  • 打赏
  • 举报
回复
str已经读取完了还怎么反序列化啊?下面这些代码删掉看看
StreamReader reader = new StreamReader(str);
byte[] mbyte = new byte[100000];
int allmybyte = (int)mbyte.Length;
int startmbyte = 0;
//statusBar.Text = \"正在接收数据...\";
while (allmybyte > 0)
{
int m = str.Read(mbyte, startmbyte, allmybyte);
if (m == 0)
break;

startmbyte += m;
allmybyte -= m;
}
龙宜坡 2009-04-01
  • 打赏
  • 举报
回复
没什么技巧可言,调试吧
cja03 2009-04-01
  • 打赏
  • 举报
回复
把一个文件转成一个ServiceItem对象???????????

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧