c# 网站返回序列化二进制客户端接收与提交

网站中部分代码:

 protected void Page_Load(object sender, EventArgs e)
    {
        Stream stream = Request.InputStream;
        byte[] buffer = new byte[Convert.ToInt32(stream.Length)];
        if (buffer.Length > 0)
        {
            stream.Read(buffer, 0, buffer.Length);
            string mac = lycommon.Serialization.GetObjectFromSerialization(buffer).ToString();
            lycommon.Users userinfo = (lycommon.Users)lycommon.Serialization.GetObjectFromSerialization(buffer);

            Dao.Users u = new Dao.Users();
            int id = u.Set(userinfo);
            userinfo.ID = id;

            List<lycommon.SClass> _myclass = u.GetClass(userinfo);

            object[] objresult = new object[] { userinfo, _myclass };

            byte[] bresult = lycommon.Serialization.ToSerialization(objresult);

            Response.BinaryWrite(bresult);
        }
        else
        {
            Response.Write("no data");
        }
        Response.End();
    }

客户端部分代码:

 

 string mac = "1C:65:9D:43:3C:09";
        lycommon.Users userinfo = new lycommon.Users();
        userinfo.Mac = mac;

        byte[] bmac = lycommon.Serialization.ToSerialization(userinfo);

        System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://localhost/toclient/zy_user.aspx");
        req.Proxy = null;
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = bmac.Length;
        System.IO.Stream netStream = req.GetRequestStream();
        netStream.Write(bmac, 0, bmac.Length);
        netStream.Close();
        System.Net.HttpWebResponse rep = (System.Net.HttpWebResponse)req.GetResponse();
       
        Stream getStream = rep.GetResponseStream();
        MemoryStream ms = new MemoryStream();
        byte[] bresult = new byte[1024];
        int bytes;
        while ((bytes = getStream.Read(bresult, 0, bresult.Length)) > 0)
        {
            ms.Write(bresult, 0, bytes);
        }
        bresult = ms.GetBuffer();
        ms.Close();
        object[] objresult = (object[])lycommon.Serialization.GetObjectFromSerialization(bresult);
        lycommon.Users result = (lycommon.Users)objresult[0];
        List<lycommon.SClass> _myclass = (List<lycommon.SClass>)objresult[1];

        rep.Close();

        foreach (lycommon.SClass sclass in _myclass)
        {
        }
发送与接收的都是序列化后的二进制数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值