Beispiel #1
0
        public XNetResult SendSynch()
        {
            if (ow || civ || dn != null)
            {
                return(new XNetResult(false));
            }
            or = ow = true;

            string dstr = "", strs = "";

            try
            {
                r = this.preSend(out dstr);
                if (r == null)
                {
                    throw new Exception("Error: " + dstr);
                }
                byte[] bdata = Encoding.UTF8.GetBytes(dstr);

                Stream          strm = null;
                HttpWebResponse res  = null;

                r.ReadWriteTimeout = (int)_opt["ReadWriteTimeout", new JSON(1000 * 60 * 15)].NumberValue;
                r.Timeout          = (int)_opt["Timeout", new JSON(1000 * 60 * 15)].NumberValue;

                if (dstr.Length > 0)
                {
                    // getting stream
                    try
                    {
                        strm = r.GetRequestStream();
                    }
                    catch (Exception ex) { throw new Exception("Cannot get request stream, details: " + ex.Message); }

                    // writing stream
                    try
                    {
                        strm.Write(bdata, 0, bdata.Length);
                    }
                    catch (Exception ex) { throw new Exception("Cannot sending data, details: " + ex.Message); }

                    strm.Close();
                }

                // get response
                try
                {
                    res = (HttpWebResponse)r.GetResponse();
                }
                catch (Exception ex) { throw new Exception("Cannot getting data, details: " + ex.Message); }

                // reading stream
                try
                {
                    using (var ssr = new StreamReader(res.GetResponseStream())) strs = ssr.ReadToEnd();
                }
                catch (Exception ex) { throw new Exception("Cannot reading data, details: " + ex.Message); }

                if (strs.Length > 0)
                {
                    dn = new XNetResult(true, res, "", strs);
                }
                else
                {
                    dn = new XNetResult(true, res);
                }

                ow = false;
                return(dn);
            }
            catch (Exception ex)
            {
                dn = new XNetResult(ex);
                return(dn);
            }
        }
Beispiel #2
0
        public bool SendAsych(Action <XNetResult> cb)
        {
            if (ow || civ || dn != null)
            {
                return(false);
            }
            or = ow = true;

            string dstr = "";

            try
            {
                r = this.preSend(out dstr);
                if (r == null)
                {
                    throw new Exception("Error: " + dstr);
                }
                byte[] bdata = Encoding.UTF8.GetBytes(dstr);

                string           strs = "";
                BackgroundWorker bgw  = new BackgroundWorker();
                bgw.DoWork += (a, b) =>
                {
                    Stream          strm     = null;
                    HttpWebResponse res      = null;
                    bool            syncDone = false;

                    r.ReadWriteTimeout = (int)_opt["ReadWriteTimeout", new JSON(1000 * 60 * 15)].NumberValue;
                    r.Timeout          = (int)_opt["Timeout", new JSON(1000 * 60 * 15)].NumberValue;

                    if (dstr.Length > 0)
                    {
                        // getting stream
                        syncDone = false;
                        try
                        {
                            var ssync = r.BeginGetRequestStream(new AsyncCallback((c) =>
                            {
                                try
                                {
                                    strm = r.EndGetRequestStream(c);
                                }
                                catch (Exception ex) { b.Result = ex.Message; }
                                finally { syncDone = true; }
                            }), r);
                        }
                        catch (Exception ex) { b.Result = ex.Message; }
                        if (b.Result != null)
                        {
                            throw new Exception("Cannot get request stream, details: " + b.Result);
                        }

                        // wait until done with checking
                        while (!syncDone)
                        {
                            if (civ)
                            {
                                b.Cancel = true;
                                r.Abort();
                                return;
                            }
                        }
                        if (b.Result != null)
                        {
                            throw new Exception("Cannot get request stream, details: " + b.Result);
                        }

                        // writing stream
                        syncDone = false;
                        var wsync = strm.BeginWrite(bdata, 0, bdata.Length, new AsyncCallback((c) =>
                        {
                            try
                            {
                                strm.EndWrite(c);
                            }
                            catch (Exception ex) { b.Result = ex.Message; }
                            finally { syncDone = true; }
                        }), strm);
                        if (b.Result != null)
                        {
                            throw new Exception("Cannot sending data, details: " + b.Result);
                        }

                        // wait until done with checking
                        while (!syncDone)
                        {
                            if (civ)
                            {
                                b.Cancel = true;
                                r.Abort();
                                return;
                            }
                        }
                        if (b.Result != null)
                        {
                            throw new Exception("Cannot sending data, details: " + b.Result);
                        }
                        strm.Close();
                    }

                    // get response
                    syncDone = false;
                    var rsync = r.BeginGetResponse(new AsyncCallback((c) =>
                    {
                        try
                        {
                            res = (HttpWebResponse)r.EndGetResponse(c);
                        }
                        catch (Exception ex) { b.Result = ex.Message; }
                        finally { syncDone = true; }
                    }), r);
                    if (b.Result != null)
                    {
                        throw new Exception("Cannot getting data, details: " + b.Result);
                    }

                    // wait until done with checking
                    while (!syncDone)
                    {
                        if (civ)
                        {
                            b.Cancel = true;
                            r.Abort();
                            return;
                        }
                    }
                    if (b.Result != null)
                    {
                        throw new Exception("Cannot getting data, details: " + b.Result);
                    }

                    // reading stream
                    using (var ssr = new StreamReader(res.GetResponseStream()))
                    {
                        strs = ssr.ReadToEnd();
                    }
                    b.Result = res;
                };

                bgw.RunWorkerCompleted += (a, b) =>
                {
                    or = false;
                    if (b.Error != null)
                    {
                        dn = new XNetResult(b.Error);
                    }
                    else if (b.Result == null || b.Cancelled)
                    {
                        dn = new XNetResult(false);
                    }
                    else if (b.Result.Equals(true))
                    {
                        dn = new XNetResult(true, "No output", "");
                    }
                    else if (b.Result.GetType() == typeof(HttpWebResponse))
                    {
                        if (strs.Length > 0)
                        {
                            dn = new XNetResult(true, (HttpWebResponse)b.Result, "", strs);
                        }
                        else
                        {
                            dn = new XNetResult(true, (HttpWebResponse)b.Result);
                        }
                    }
                    else
                    {
                        dn = new XNetResult(false);
                    }

                    cb(dn);
                    ow = false;
                };

                bgw.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                dn = new XNetResult(ex);
                cb(dn);
            }
            return(true);
        }