C#实现手机端和服务器端的数据交互

it2022-05-27  66

1,首先我们必须要引用命名空间  using System.Net; 2,使用该命名空间下的 HttpwebRequest 对象实现请求服务器的页面 例如:             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);             request.Method = "POST";             request.ContentLength = bytes.Length;             request.ContentType = "application/x-www-form-urlencoded";              3,当我们手机端 需要服务器端相应的消息时,我们可以使用HttpWebResponse 对象来获得相应。 4,获得相应后,我们手机端需要把服务器端相应发送过来的数据流读取出来,因此我们要引用 using System.IO此命名空间下StreamReader 来读取数据流中的数据。例如:                     HttpWebResponse response = (HttpWebResponse)request.GetResponse();                     StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF7);                     contentBack = reader.ReadLine(); 5,如果我们想向服务器发送数据,就要将数据以流的形式发送到服务器端。例如:                     Stream requestStream = request.GetRequestStream()                     requestStream.Write(bytes, 0, bytes.Length);                     requestStream.Close(); 6,现在需要让服务器端的接收到数据 需要用到Request.InputStream 将流读取出来;例如                 Stream str = Request.InputStream;                 int strLen, strRead;                 strLen = System.Convert.ToInt32(str.Length);                 Byte[] strArr = new Byte[strLen];                 strRead = str.Read(strArr, 0, strLen);                 char[] strChar = System.Text.Encoding.Default.GetChars(strArr);                 string s = new String(strChar); 7,这样就实现了手机端和服务器端的数据的交互; 下面是本人写的一段比较通用的函数:供参考

 /// <summary>        /// 发送文件名称 到服务器端        /// </summary>        /// <param name="url">服务器路径</param>        /// <param name="xml">参数的值</param>        string contentBack;        public void PostFileName(string url, string name,string registerNum)        {            string postData = name + "&" + registerNum;            byte[] bytes = Encoding.UTF7.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.UTF7);                    contentBack = reader.ReadLine();                    //MessageBox.Show(contentBack, "服务器消息:");                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);            }        }

服务器端的接受:

                //获得参数的值                Stream str = Request.InputStream;                int strLen, strRead;                strLen = System.Convert.ToInt32(str.Length);                Byte[] strArr = new Byte[strLen];                strRead = str.Read(strArr, 0, strLen);                char[] strChar = System.Text.Encoding.Default.GetChars(strArr);                string s = new String(strChar);

                if (s =="")                {                    return;                }

                //切割参数                string[] strArray = s.Split(new char[]{'&'});                string imei =strArray[0];                string code =strArray[1];

转载于:https://www.cnblogs.com/hxycn/archive/2009/08/21/1551551.html


最新回复(0)