コード例 #1
0
            public void Create()
            {
                LogManager log = LogManager.GetLogger(Global.CallerName("", 0, ""));

                try
                {
                    log.Info(string.Format("xhr open {0}: {1}", (object)this.Method, (object)this.Uri));
                    this.Xhr        = (HttpWebRequest)WebRequest.Create(this.Uri);
                    this.Xhr.Method = this.Method;
                    if (this.CookieHeaderValue != null)
                    {
                        this.Xhr.Headers.Add("Cookie", this.CookieHeaderValue);
                    }
                    if (this.ExtraHeaders != null)
                    {
                        foreach (KeyValuePair <string, string> extraHeader in this.ExtraHeaders)
                        {
                            this.Xhr.Headers.Add(extraHeader.Key, extraHeader.Value);
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                    this.OnError(ex);
                    return;
                }
                if (this.Method == "POST")
                {
                    this.Xhr.ContentType = "application/octet-stream";
                }
                try
                {
                    if (this.Data != null)
                    {
                        this.Xhr.ContentLength = (long)this.Data.Length;
                        using (Stream requestStream = this.Xhr.GetRequestStream())
                            requestStream.Write(this.Data, 0, this.Data.Length);
                    }
                    EasyTimer.TaskRun((ActionTrigger)(() =>
                    {
                        LogManager logger = LogManager.GetLogger(Global.CallerName("", 0, ""));
                        logger.Info("Task.Run Create start");
                        using (WebResponse response = this.Xhr.GetResponse())
                        {
                            log.Info("Xhr.GetResponse ");
                            Dictionary <string, string> headers = new Dictionary <string, string>();
                            for (int index = 0; index < response.Headers.Count; ++index)
                            {
                                headers.Add(response.Headers.Keys[index], response.Headers[index]);
                            }
                            this.OnResponseHeaders(headers);
                            string header = response.Headers["Content-Type"];
                            using (Stream responseStream = response.GetResponseStream())
                            {
                                Debug.Assert(responseStream != null, "resStream != null");
                                if (header.Equals("application/octet-stream", StringComparison.OrdinalIgnoreCase))
                                {
                                    byte[] buffer = new byte[16384];
                                    using (MemoryStream memoryStream = new MemoryStream())
                                    {
                                        int count;
                                        while ((count = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                                        {
                                            memoryStream.Write(buffer, 0, count);
                                        }
                                        this.OnData(memoryStream.ToArray());
                                    }
                                }
                                else
                                {
                                    using (StreamReader streamReader = new StreamReader(responseStream))
                                        this.OnData(streamReader.ReadToEnd());
                                }
                            }
                        }
                        logger.Info("Task.Run Create finish");
                    }));
                }
                catch (IOException ex)
                {
                    log.Error("Create call failed", (Exception)ex);
                    this.OnError((Exception)ex);
                }
                catch (WebException ex)
                {
                    log.Error("Create call failed", (Exception)ex);
                    this.OnError((Exception)ex);
                }
                catch (Exception ex)
                {
                    log.Error("Create call failed", ex);
                    this.OnError(ex);
                }
            }
コード例 #2
0
            public void Create()
            {
                var log = LogManager.GetLogger(Global.CallerName());

                try
                {
                    log.Info(string.Format("xhr open {0}: {1}", Method, Uri));
                    Xhr             = (HttpWebRequest)WebRequest.Create(Uri);
                    Xhr.Method      = Method;
                    Xhr.Credentials = CredentialCache.DefaultNetworkCredentials;
                    if (CookieHeaderValue != null)
                    {
                        Xhr.Headers.Add("Cookie", CookieHeaderValue);
                        log.Info("added header " + CookieHeaderValue);
                    }
                    else
                    {
                        log.Info("not added header " + CookieHeaderValue);
                    }
                }
                catch (Exception e)
                {
                    log.Error(e);
                    OnError(e);
                    return;
                }


                if (Method == "POST")
                {
                    Xhr.ContentType = "application/octet-stream";
                }

                try
                {
                    if (Data != null)
                    {
                        Xhr.ContentLength = Data.Length;

                        using (var requestStream = Xhr.GetRequestStream())
                        {
                            requestStream.Write(Data, 0, Data.Length);
                        }
                    }

                    EasyTimer.TaskRun(() =>
                    {
                        var log2 = LogManager.GetLogger(Global.CallerName());
                        log2.Info("Task.Run Create start");
                        try
                        {
                            using (var res = Xhr.GetResponse())
                            {
                                log.Info("Xhr.GetResponse ");

                                var responseHeaders = new Dictionary <string, string>();
                                for (int i = 0; i < res.Headers.Count; i++)
                                {
                                    responseHeaders.Add(res.Headers.Keys[i], res.Headers[i]);
                                }
                                OnResponseHeaders(responseHeaders);

                                var contentType = res.Headers["Content-Type"];



                                using (var resStream = res.GetResponseStream())
                                {
                                    Debug.Assert(resStream != null, "resStream != null");
                                    if (contentType.Equals("application/octet-stream",
                                                           StringComparison.OrdinalIgnoreCase))
                                    {
                                        var buffer = new byte[16 * 1024];
                                        using (var ms = new MemoryStream())
                                        {
                                            int read;
                                            while ((read = resStream.Read(buffer, 0, buffer.Length)) > 0)
                                            {
                                                ms.Write(buffer, 0, read);
                                            }
                                            var a = ms.ToArray();
                                            OnData(a);
                                        }
                                    }
                                    else
                                    {
                                        using (var sr = new StreamReader(resStream))
                                        {
                                            OnData(sr.ReadToEnd());
                                        }
                                    }
                                }
                            }
                            log2.Info("Task.Run Create finish");
                        }
                        catch (System.Net.WebException e)
                        {
                            log2.Error("Task.Run Create failed", e);
                            OnError(e);
                        }
                    });
                }
                catch (System.IO.IOException e)
                {
                    log.Error("Create call failed", e);
                    OnError(e);
                }
                catch (System.Net.WebException e)
                {
                    log.Error("Create call failed", e);
                    OnError(e);
                }
                catch (Exception e)
                {
                    log.Error("Create call failed", e);
                    OnError(e);
                }
            }