Ejemplo n.º 1
0
 void ReciveData(Stream st, HttpDesc desc, Action <string> onSuceess = null, Action <string> onFail = null)
 {
     using (StreamReader rd = new StreamReader(st))
     {
         string data = rd.ReadToEnd();
         desc.Fill(true, data, onSuceess, onFail);
     }
 }
Ejemplo n.º 2
0
        public void Post(string url, byte[] data, Action <string> onSuceess = null, Action <string> onFail = null, Dictionary <string, string> headers = null)
        {
            Action ac = new Action(() =>
            {
                HttpDesc desc = FindFreeCache();
                try
                {
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                    req.Timeout        = 5000;
                    if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                    {
                        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); });
                        req.ProtocolVersion = HttpVersion.Version11;
                    }
                    req.Method = "POST";
                    if (headers != null && headers.Count > 0)
                    {
                        BuildHeaders(req, headers);
                    }
                    if (data != null && data.Length > 0)
                    {
                        using (Stream stream = req.GetRequestStream())
                        {
                            stream.Write(data, 0, data.Length);
                        }
                    }

                    using (WebResponse wr = req.GetResponse())
                    {
                        //在这里对接收到的页面内容进行处理
                        using (Stream st = wr.GetResponseStream())
                        {
                            ReciveData(st, desc, onSuceess, onFail);
                            AddToQueue(desc);
                        }
                    }
                }
                catch (Exception ex)
                {
                    desc.Fill(false, ex.Message, onSuceess, onFail);
                    AddToQueue(desc);
                }
            });

            ac.BeginInvoke((IAsyncResult ar) =>
            {
                if (ar.IsCompleted)
                {
                    Action result = (Action)ar.AsyncState;
                    if (result != null)
                    {
                        result.EndInvoke(ar);
                    }
                }
            }, ac);
        }
Ejemplo n.º 3
0
        public void InitDataCache()
        {
            int      i = 0;
            HttpDesc desc;

            while (i < HttpDesc.cfg_CacheCount)
            {
                desc = new HttpDesc();
                m_DataCache.Enqueue(desc);
                i++;
            }
        }
Ejemplo n.º 4
0
 private void Update()
 {
     lock (HttpDesc.cfg_LockObj)
     {
         if (m_CallBackList.Count > 0)
         {
             while (m_CallBackList.Count > 0)
             {
                 HttpDesc desc = m_CallBackList.Dequeue();
                 desc.Excute();
                 AddToCache(desc);
             }
         }
     }
 }
Ejemplo n.º 5
0
        private HttpDesc FindFreeCache()
        {
            HttpDesc desc = null;

            lock (HttpDesc.cfg_LockObj)
            {
                if (m_DataCache.Count > 0)
                {
                    desc = m_DataCache.Dequeue();
                }
                else
                {
                    desc = new HttpDesc();
                }
            }

            return(desc);
        }
Ejemplo n.º 6
0
        public void Get(string url, Action <string> onSuceess = null, Action <string> onFail = null)
        {
            Action ac = new Action(() =>
            {
                HttpDesc desc = FindFreeCache();
                try
                {
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                    req.Method         = "GET";
                    using (WebResponse wr = req.GetResponse())
                    {
                        //在这里对接收到的页面内容进行处理
                        using (Stream st = wr.GetResponseStream())
                        {
                            ReciveData(st, desc, onSuceess, onFail);
                            AddToQueue(desc);
                        }
                    }
                }
                catch (Exception ex)
                {
                    desc.Fill(false, ex.Message, onSuceess, onFail);
                    AddToQueue(desc);
                }
            });

            ac.BeginInvoke((IAsyncResult ar) =>
            {
                if (ar.IsCompleted)
                {
                    Action result = (Action)ar.AsyncState;
                    if (result != null)
                    {
                        result.EndInvoke(ar);
                    }
                }
            }, ac);
        }
Ejemplo n.º 7
0
 public void AddToCache(HttpDesc desc)
 {
     m_DataCache.Enqueue(desc);
 }
Ejemplo n.º 8
0
 public void AddToQueue(HttpDesc desc)
 {
     m_CallBackList.Enqueue(desc);
 }