Example #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);
     }
 }
Example #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);
        }
Example #3
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);
        }