手机端和服务器端的通信

发表于:2009-11-02 11:34

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

#
DotNet

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

  }

  }

31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号