Exemple #1
0
        public static void GetHttp(string Url, Action <string> onDone, Action <HttpStatusCode> onFail)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            HttpWebRequest  request  = null;
            HttpWebResponse response = null;

            try
            {
                request = (HttpWebRequest)WebRequest.Create(Url);
            }
            catch (Exception)
            {
                onFail(HttpStatusCode.NotAcceptable);
                return;
            }
            stopwatch.Stop();
            uint timerId = TimerHeap.AddTimer(0x3a98, 0, () => onFail(HttpStatusCode.RequestTimeout));

            stopwatch.Start();
            response = (HttpWebResponse)request.GetResponse();
            stopwatch.Stop();
            if (response.StatusCode != HttpStatusCode.OK)
            {
                stopwatch.Start();
                TimerHeap.DelTimer(timerId);
                onFail(response.StatusCode);
                stopwatch.Stop();
            }
            else
            {
                TimerHeap.DelTimer(timerId);
                stopwatch.Start();
                Stream responseStream = response.GetResponseStream();
                stopwatch.Stop();
                stopwatch.Start();
                Encoding      encoding = Encoding.UTF8;
                StreamReader  reader   = new StreamReader(responseStream, encoding);
                char[]        buffer   = new char[0x100];
                int           length   = reader.Read(buffer, 0, 0x100);
                StringBuilder builder  = new StringBuilder("");
                while (length > 0)
                {
                    string str = new string(buffer, 0, length);
                    builder.Append(str);
                    length = reader.Read(buffer, 0, 0x100);
                }
                stopwatch.Stop();
                stopwatch.Start();
                response.Close();
                reader.Close();
                stopwatch.Stop();
                stopwatch.Start();
                onDone(builder.ToString());
                stopwatch.Stop();
            }
        }
Exemple #2
0
 /**关闭连接**/
 public void Close()
 {
     stopReceive();
     //OnNetworkDisconnected = null;
     if (client != null)
     {
         client.Close();
     }
     if (timeId > 0)
     {
         TimerHeap.DelTimer(timeId);
     }
     LoggerHelper.Debug("-close() connected:" + Connected() + " 已断开与服务器[" + ip + ":" + port + "]连接!");
 }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="datastr"></param>
        /// <returns>返回字符串</returns>
        public static void SendPostHttp(string Url, string datastr, Action <string> onDone, Action <HttpStatusCode> onFail)
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes(datastr);
            // 准备请求...
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);

            req.Method        = "Post"; //Getor Post
            req.ContentType   = "application/x-www-form-urlencoded";
            req.ContentLength = data.Length;
            Stream stream = req.GetRequestStream();

            // 发送数据
            stream.Write(data, 0, data.Length);
            stream.Close();

            uint            timerId = TimerHeap.AddTimer(15000, 0, () => { onFail(HttpStatusCode.RequestTimeout); });
            HttpWebResponse rep     = (HttpWebResponse)req.GetResponse();


            if (rep.StatusCode != HttpStatusCode.OK)
            {
                TimerHeap.DelTimer(timerId);
                onFail(rep.StatusCode);
            }
            else
            {
                TimerHeap.DelTimer(timerId);
                Stream   receiveStream = rep.GetResponseStream();
                Encoding encode        = System.Text.Encoding.UTF8;
                // Pipes the stream to a higher level stream reader with the required encoding format.
                StreamReader readStream = new StreamReader(receiveStream, encode);

                Char[]        read  = new Char[256];
                int           count = readStream.Read(read, 0, 256);
                StringBuilder sb    = new StringBuilder("");
                while (count > 0)
                {
                    String readstr = new String(read, 0, count);
                    sb.Append(readstr);
                    count = readStream.Read(read, 0, 256);
                }

                rep.Close();
                readStream.Close();

                onDone(sb.ToString());
            }
        }
Exemple #4
0
        public static void SendPostHttp(string Url, string datastr, Action <string> onDone, Action <HttpStatusCode> onFail)
        {
            byte[]         bytes   = Encoding.UTF8.GetBytes(datastr);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

            request.Method        = "Post";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = bytes.Length;
            Stream requestStream = request.GetRequestStream();

            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            uint            timerId  = TimerHeap.AddTimer(0x3a98, 0, () => onFail(HttpStatusCode.RequestTimeout));
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                TimerHeap.DelTimer(timerId);
                onFail(response.StatusCode);
            }
            else
            {
                TimerHeap.DelTimer(timerId);
                Stream        responseStream = response.GetResponseStream();
                Encoding      encoding       = Encoding.UTF8;
                StreamReader  reader         = new StreamReader(responseStream, encoding);
                char[]        buffer         = new char[0x100];
                int           length         = reader.Read(buffer, 0, 0x100);
                StringBuilder builder        = new StringBuilder("");
                while (length > 0)
                {
                    string str = new string(buffer, 0, length);
                    builder.Append(str);
                    length = reader.Read(buffer, 0, 0x100);
                }
                response.Close();
                reader.Close();
                onDone(builder.ToString());
            }
        }
Exemple #5
0
        public static void GetHttp(string Url, Action <string> onDone, Action <HttpStatusCode> onFail)
        {
            //Debug.LogError("GetHttp");
            // 准备请求...
            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            stopWatch.Start();
            HttpWebRequest  req = null;
            HttpWebResponse rep = null;

            try
            {
                req = (HttpWebRequest)WebRequest.Create(Url);
            }
            catch (Exception e)
            {
                //Debug.LogError(e.ToString());
                onFail(HttpStatusCode.NotAcceptable);
                return;
            }

            stopWatch.Stop();
            //Debug.LogError("req Create:" + stopWatch.ElapsedMilliseconds);

            uint timerId = TimerHeap.AddTimer(15000, 0, () => { onFail(HttpStatusCode.RequestTimeout); });

            stopWatch.Start();
            rep = (HttpWebResponse)req.GetResponse();
            stopWatch.Stop();
            //Debug.LogError("req GetResponse:" + stopWatch.ElapsedMilliseconds);

            if (rep.StatusCode != HttpStatusCode.OK)
            {
                stopWatch.Start();
                TimerHeap.DelTimer(timerId);
                onFail(rep.StatusCode);
                stopWatch.Stop();
                //Debug.LogError("req onFail:" + stopWatch.ElapsedMilliseconds);
            }
            else
            {
                TimerHeap.DelTimer(timerId);
                stopWatch.Start();
                Stream receiveStream = rep.GetResponseStream();
                stopWatch.Stop();
                //Debug.LogError("req GetResponseStream:" + stopWatch.ElapsedMilliseconds);

                stopWatch.Start();
                Encoding encode = System.Text.Encoding.UTF8;
                // Pipes the stream to a higher level stream reader with the required encoding format.
                StreamReader readStream = new StreamReader(receiveStream, encode);

                Char[]        read  = new Char[256];
                int           count = readStream.Read(read, 0, 256);
                StringBuilder sb    = new StringBuilder("");
                while (count > 0)
                {
                    String readstr = new String(read, 0, count);
                    sb.Append(readstr);
                    count = readStream.Read(read, 0, 256);
                }

                stopWatch.Stop();
                //Debug.LogError("req readStream:" + stopWatch.ElapsedMilliseconds);

                stopWatch.Start();
                rep.Close();
                readStream.Close();
                stopWatch.Stop();
                //Debug.LogError("req Close:" + stopWatch.ElapsedMilliseconds);


                stopWatch.Start();
                onDone(sb.ToString());
                stopWatch.Stop();
                //Debug.LogError("req onDone:" + stopWatch.ElapsedMilliseconds);
            }
        }
Exemple #6
0
 // 代替析构函数
 public void ReleaseMogoTimeData()
 {
     TimerHeap.DelTimer(timer);
     TimerHeap.DelTimer(escapeTimer);
 }