public TransactionReportClient(Guid merchantKey, Uri hostUri) {

            if (merchantKey == Guid.Empty) {
                merchantKey = ConfigurationUtility.GetConfigurationKey("MerchantKey");
            }
            this.MerchantKey = merchantKey;

            if (hostUri == null) { throw new ArgumentNullException("hostUri"); }
            this.HostUri = hostUri;

            this.HttpUtility = new HttpUtility();
        }
Ejemplo n.º 2
0
        public static void DownloadFile(HttpContext context, string filePath)
        {
            HttpRequest  Request  = context.Request;
            HttpResponse Response = context.Response;

            Response.Clear();
            Response.Buffer = false;
            Response.AddHeader("Accept-Ranges", "bytes");
            Response.ContentType = MediaTypeNames.Application.Octet;
            using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                long offset = 0L, length = stream.Length;
                #region Resolve Range
                string range = Request.Headers["Range"];
                int    index;
                //Range:bytes=1024-
                //Range:bytes=1024-2048
                //Range:bytes=-1024
                //Range:bytes=0-512,1024-2048
                if (range != null && (index = range.IndexOf("=")) != -1)
                {
                    string[] ranges = range.Substring(index + 1).Split(',');
                    if (ranges.Length > 1)
                    {
                        Response.StatusCode = 416;  //not supported multipart/byterange
                        Response.End();
                    }

                    bool flag = false;
                    if (ranges[0].StartsWith("-"))
                    {
                        long _p, _absp;
                        if (long.TryParse(ranges[0], out _p) && (_absp = Math.Abs(_p)) <= length)
                        {
                            if (_p < 0)
                            {
                                offset = length - _absp;
                                length = _absp;
                                flag   = true;
                            }
                        }
                    }
                    else
                    {
                        ranges = ranges[0].Split('-');
                        if (ranges.Length == 2)
                        {
                            long _p, _l;
                            if (ranges[1] == string.Empty)
                            {
                                if (long.TryParse(ranges[0], out _p) && _p <= length)
                                {
                                    offset = _p;
                                    flag   = true;
                                }
                            }
                            else if (long.TryParse(ranges[0], out _p) && long.TryParse(ranges[1], out _l) && _p > 0 && _l > 0 && _p < _l && _l < length)
                            {
                                offset = _p;
                                length = _l + 1;
                                flag   = true;
                            }
                        }
                    }
                    if (!flag)
                    {
                        Response.StatusCode = 416;  //Requested range not satisfiable
                        Response.End();
                    }
                    Response.StatusCode = 206;
                    Response.AddHeader("Content-Range", "bytes " + offset.ToString() + "-" + length.ToString() + "/" + stream.Length.ToString());
                }
                #endregion
                Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(filePath)));
                Response.AddHeader("Content-Length", length.ToString());
                Response.TransmitFile(filePath, offset, length);
            }
            Response.End();
        }