这个问题有点复杂,请教高手
我将一个类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对象的各个属性。可是老是不成功。麻烦哪位高手能帮我指点迷津,谢谢啦!