• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

手机端和服务器端的通信【C#】

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 【1】手机端和服务器端简单的数据同步

        /// <summary>
        /// 首先传参数(strImei,strCode)到服务器然后,下载LocalAllCategory.Xml到手机端-------方法
        /// </summary>
        /// <param name="URL">服务器URL地址</param>
        /// <param name="Filename">存放到本地的路径</param>
        /// <param name="strSortName">类别名称</param>
        /// <param name="strRightMark">权限码</param>
        public static void DownAllCategory(string URL, string Filename, string strImei, string strCode)
        {
            //向服务器传参数
            string postData = strImei+"&"+strCode;
            byte[] bytes = Encoding.UTF7.GetBytes(postData);          //注意中文参数需要UTF7编码方式
            HttpWebRequest Myrq = (HttpWebRequest)WebRequest.Create(URL);
            Myrq.Method = "POST";
            Myrq.ContentLength = bytes.Length;
            Myrq.ContentType = "application/x-www-form-urlencoded";
            try
            {
                using (Stream requestStream = Myrq.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                }
                //下载文件开始
                HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;
                //Prog.Maximum = (int)totalBytes;
                Stream st = myrp.GetResponseStream();
                Stream so = new FileStream(Filename, FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    Application.DoEvents();
                    so.Write(by, 0, osize);
                    //Prog.Value = (int)totalDownloadedByte;
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();
                //下载文件结束
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk,
                    MessageBoxDefaultButton.Button1);
            }

        }

 

【2】下载文件【Http通信】

  

/// <summary>
        /// 下载方法(带进度条)
        /// </summary>
        /// <param name="URL">服务器URL地址</param>
        /// <param name="Filename">存放到本地的路径</param>
        /// <param name="Prog">进度条</param>
        public string DownCategoryFile(string URL, string Filename, ProgressBar Prog)
        {
            try
            {
                HttpWebRequest Myrq = (HttpWebRequest)HttpWebRequest.Create(URL);

                HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;
                Prog.Maximum = (int)totalBytes;
                Stream st = myrp.GetResponseStream();
                Stream so = new FileStream(Filename, FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    Application.DoEvents();
                    so.Write(by, 0, osize);
                    Prog.Value = (int)totalDownloadedByte;
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();
                return "下载成功";
                //MessageBox.Show("更新完毕!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
            catch
            {
                return "下载失败";
            }

        }

【3】发送数据到服务器端的方法

        /// <summary>
        /// 发送数据到服务器端的方法
        /// </summary>
        /// <param name="url">服务器路径</param>
        /// <param name="xml">参数的值</param>
        public string  PostXml(string url, string strImei,string strCode)
        {
            string postData =strImei+"&"+strCode;
    
            byte[] bytes = Encoding.UTF8.GetBytes(postData);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentLength = bytes.Length;
            request.ContentType = "application/x-www-form-urlencoded";
           
            try
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();

                    //响应请求
                    HttpWebResponse response =(HttpWebResponse)request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    content = reader.ReadLine();
                    return content;
                    //MessageBox.Show(content,"提示:",MessageBoxButtons.OK,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
                }
            }
            catch
            {
                return "失败";
                //MessageBox.Show(ex.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
            }
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#里的实现confirm发布时间:2022-07-10
下一篇:
c#截取字符串发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap