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