当前位置:编程学习 > C#/ASP.NET >>

C#实现手机端和服务器端的通信

以下代码只供参考,大家有兴趣可以研究一下。

【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

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,