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

it2022-05-26  73

 【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);            }        }

 

转载于:https://www.cnblogs.com/hxycn/archive/2009/11/02/1594336.html

相关资源:C#和Android通信实例程序(含源码)

最新回复(0)