예제 #1
0
        public static string Process(DownloadItem downloadItem, string cachePath)
        {
            var regexMatch = Regex.Match(downloadItem.Url, MatchRegex);

            var    internalType = regexMatch.Groups["internalType"].Value;
            var    idType       = regexMatch.Groups["idType"].Value;
            var    id           = regexMatch.Groups["id"].Value;
            string output       = string.Empty;

            switch (internalType)
            {
            case "movieMeterHandler":
                output = Tools.ThirdParty.MovieMeterApiHandler.GenerateMovieMeterXml(downloadItem, idType, id);
                break;
            }

            if (!string.IsNullOrEmpty(output))
            {
                File.WriteAllText(cachePath + ".txt.tmp", output, Encoding.UTF8);
                Gzip.Compress(cachePath + ".txt.tmp", cachePath + ".txt.gz");
                File.Delete(cachePath + ".txt.tmp");
            }

            return(output);
        }
        public void BasicByteArrayTest(string value)
        {
            var compressed   = Gzip.Compress(value);
            var uncompressed = Gzip.DecompressToString(compressed);

            Assert.Equal(value, uncompressed);
        }
예제 #3
0
        private static void Main(string[] args)
        {
            try
            {
                bool isCompress = true;

                if (args.Length < 3)
                {
                    throw new Exception("Вы не ввели команду программы,имя исходного и результирующего файла");
                }

                if (args[0] != "compress")
                {
                    isCompress = false;
                    if (args[0] != "decompress")
                    {
                        throw new Exception($"Команда \"{args[0]}\" не существует");
                    }
                }

                if (args.Length < 2)
                {
                    throw new Exception("Вы не ввели имя исходного и результирующего файла");
                }

                if (args.Length < 3)
                {
                    throw new Exception("Вы не ввели имя результирующего файла");
                }

                var readingFile = args[1];
                var writingFile = args[2];

                var gzip = new Gzip();

                if (isCompress)
                {
                    gzip.Compress(readingFile, writingFile);
                }
                else
                {
                    gzip.Decompress(readingFile, writingFile);
                }

                Console.WriteLine("0");
                Console.ReadKey();
            }

            catch (Exception e)
            {
                Console.WriteLine($"Error message: {e.Message}");
                Console.WriteLine("1");
                Console.ReadKey();
            }
        }
예제 #4
0
        public void GzippingWorks()
        {
            var input        = "{'IsPrimitive':true,'IsArray':true,'IsNumeric':false,'Type':'Array','Value':[{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'65'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'80'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'71'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'33'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'48'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'81'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'94'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'67'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'26'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'55'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'29'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'68'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'53'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'53'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'30'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'84'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'15'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'25'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'76'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'45'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'49'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'96'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'4'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'40'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'84'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'39'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'73'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'1'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'18'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'6'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'14'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'57'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'71'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'33'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'23'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'32'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'50'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'14'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'74'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'24'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'45'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'52'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'90'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'40'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'33'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'66'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'38'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'28'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'10'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'98'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'81'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'45'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'76'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'79'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'91'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'69'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'76'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'32'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'49'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'9'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'8'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'94'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'49'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'21'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'42'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'72'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'97'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'21'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'31'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'98'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'34'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'2'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'50'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'52'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'1'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'96'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'6'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'63'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'7'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'18'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'2'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'46'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'90'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'19'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'24'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'68'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'80'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'91'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'41'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'54'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'55'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'19'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'64'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'66'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'16'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'50'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'4'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'70'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'98'},{'IsPrimitive':true,'IsArray':false,'IsNumeric':true,'Type':'Int32','Value':'60'}]}";
            var gzipped      = Gzip.Compress(input);
            var decompressed = Gzip.Decompress(gzipped);

            Assert.AreEqual(decompressed, input);

            double inputByteCount   = Encoding.UTF8.GetBytes(input).Length;
            double gzippedByteCount = Convert.FromBase64String(gzipped).Length;

            Assert.IsTrue(gzippedByteCount < (inputByteCount * 5));
        }
        // Save and send to API
        public void OnBtnSaveRecording()
        {
            string exerciseRecordingCompressed = Convert.ToBase64String(Gzip.Compress(JsonConvert.SerializeObject(homeRevalSession.CurrentRecording.ExerciseRecording)));

            JObject exerciseJson = new JObject(
                new JProperty("name", homeRevalSession.CurrentRecording.Name),
                new JProperty("description", homeRevalSession.CurrentRecording.Description),
                new JProperty("recording", exerciseRecordingCompressed));


            /*string exercisePlanning = "{\"startDate\":\""+ homeRevalSession.CurrentRecording.StartDate.ToLongDateString() + "\", " +
             *  "\"endDate\":\"" + homeRevalSession.CurrentRecording.EndDate.ToLongDateString() + "\"" +
             *  "\"description\":\"Geen idee hebben we nog niet.\"" +
             *  "\"amount\":" + homeRevalSession.CurrentRecording.Amount + "}";*/

            Debug.Log("request begint nu!");
            StartCoroutine(requestService.Post("/exercise", exerciseJson.ToString(),
                                               success => {
                Debug.Log(success);
                JObject response = JObject.Parse(success);
                //Debug.Log(response.ToString());

                JObject exercisePlanningJson = new JObject(
                    new JProperty("startDate", homeRevalSession.CurrentRecording.StartDate.ToLongDateString()),
                    new JProperty("endDate", homeRevalSession.CurrentRecording.EndDate.ToLongDateString()),
                    new JProperty("amount", homeRevalSession.CurrentRecording.Amount),
                    new JProperty("description", "Planning description die we nog niet kunnen invullen in unity dus deze is voor nu leeg."),
                    new JProperty("userExercise", new JObject(
                                      new JProperty("user_ID", homeRevalSession.UserID.ToString()),
                                      new JProperty("exercise_ID", response.GetValue("id").ToString())
                                      ))
                    );

                Debug.Log("JSON EXERCISE PLANNING DIE IK VERZEND: " + exercisePlanningJson.ToString());

                // Save planning
                StartCoroutine(requestService.Post("/exerciseplanning", exercisePlanningJson.ToString(),
                                                   successPlanning => {
                    Debug.Log(successPlanning);
                    SceneManager.LoadScene(0);
                },
                                                   errorPlanning => {
                    Debug.Log(errorPlanning);
                }
                                                   ));
            },
                                               error => {
                Debug.Log(error);
            }
                                               ));
        }
예제 #6
0
파일: GzipTests.cs 프로젝트: nardin/gzip
        public void CompressTestCompatibility()
        {
            var rnd = new Random();

            var inputFile        = Path.GetTempFileName();
            var compressedFile   = Path.GetTempFileName();
            var decompressedFile = Path.GetTempFileName();

            try
            {
                using (var fileStream = new FileStream(inputFile, FileMode.Create))
                {
                    var buffer = new byte[3 * 1024 * 1024];
                    rnd.NextBytes(buffer);
                    fileStream.Write(buffer);
                }

                var watch = new Stopwatch();
                watch.Start();
                Gzip.Compress(inputFile, compressedFile, 1, true);
                watch.Stop();
                output.WriteLine("Compress Compatibility: " + watch.ElapsedMilliseconds);
                watch.Restart();
                Gzip.Decompress(compressedFile, decompressedFile);
                watch.Stop();
                output.WriteLine("Decompress Compatibility: " + watch.ElapsedMilliseconds);

                Assert.True(FileCompare(inputFile, decompressedFile));
            }
            finally
            {
                if (File.Exists(inputFile))
                {
                    File.Delete(inputFile);
                }

                if (File.Exists(compressedFile))
                {
                    File.Delete(compressedFile);
                }

                if (File.Exists(decompressedFile))
                {
                    File.Delete(decompressedFile);
                }
            }
        }
예제 #7
0
        public void Publish(Event @event)
        {
            if (!_connection.IsConnected)
            {
                _connection.TryConnect();
            }

            var policy = RetryPolicy.Handle <BrokerUnreachableException>()
                         .Or <SocketException>()
                         .WaitAndRetry(_retryCount,
                                       retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
            {
                _logger.LogWarning(ex.ToString());
            });

            using (var channel = _connection.CreateModel())
            {
                var eventName = @event.GetType().Name;

                channel.ExchangeDeclare(exchange: _broker, type: "direct");

                var message = JsonConvert.SerializeObject(@event);
                var body    = Encoding.UTF8.GetBytes(Gzip.Compress(message));

                policy.Execute(() =>
                {
                    var properties          = channel.CreateBasicProperties();
                    properties.DeliveryMode = 2; // persistent

                    channel.BasicPublish(exchange: _broker,
                                         routingKey: eventName,
                                         mandatory: true,
                                         basicProperties: properties,
                                         body: body);
                });
            }
        }
예제 #8
0
        void FixedUpdate()
        {
            if (_reader != null)
            {
                var frame = _reader.AcquireLatestFrame();

                if (frame != null)
                {
                    IList <Body> _bodies = new Body[frame.BodyFrameSource.BodyCount];

                    frame.GetAndRefreshBodyData(_bodies);

                    // Display only first active body
                    for (int i = 0; i < frame.BodyFrameSource.BodyCount; i++)
                    {
                        if (_bodies[i] != null)
                        {
                            if (_bodies[i].IsTracked)
                            {
                                if (_bodies[i].HandLeftState == HandState.Closed)
                                {
                                    leftHandClosed         = true;
                                    leftHandStateText.text = "Closed";
                                }
                                else //if (_bodies[i].HandLeftState == HandState.Open)
                                {
                                    leftHandClosed         = false;
                                    leftHandStateText.text = "Open";
                                }
                                if (_bodies[i].HandRightState == HandState.Closed)
                                {
                                    rightHandClosed         = true;
                                    rightHandStateText.text = "Closed";
                                }
                                else //if (_bodies[i].HandRightState == HandState.Open)
                                {
                                    rightHandClosed         = false;
                                    rightHandStateText.text = "Open";
                                }

                                bodyDrawer.DrawSkeleton(_bodies[i].Joints);

                                if (state == PractiseState.KinectChecking)
                                {
                                    ConvertedBody convertedBody = exerciseService.Convert(_bodies[i],
                                                                                          jsonExercise
                                                                                          .ExerciseRecording
                                                                                          .JointMappings);

                                    ProgressText.text = exerciseService.Progression();

                                    ExerciseScore score = exerciseService.Check(convertedBody);

                                    if (exerciseService.State() == ExerciseValidator.ValidatorState.Checking)
                                    {
                                        // Check body and add score to list
                                        exerciseResultScores.Add(score);

                                        // Add body to list for recording
                                        exerciseResultRecording.Add(convertedBody);
                                    }

                                    if (exerciseService.State() == ExerciseValidator.ValidatorState.Done)
                                    {
                                        CompletedOverlay.SetActive(true);
                                        TimerText.text = "";

                                        int endScore = 0;
                                        int total    = 0;


                                        for (int j = 0; j < exerciseResultScores.Count; j++)
                                        {
                                            total += exerciseResultScores[j].Score;
                                        }

                                        endScore = (int)Math.Round(((float)total / (float)exerciseResultScores.Count) * 25);

                                        string exerciseResultRecordingCompressed = Convert.ToBase64String(Gzip.Compress(JsonConvert.SerializeObject(exerciseResultRecording)));

                                        JObject resultJson = new JObject(
                                            new JProperty("duration", 0),
                                            new JProperty("score", endScore),
                                            new JProperty("exercisePlanning_ID", hrs.currentPlanningId),
                                            new JProperty("result", exerciseResultRecordingCompressed));

                                        ScoreText.text = baseScoreText + endScore + "%";

                                        StartCoroutine(
                                            requestService.Post("/exerciseresult", resultJson.ToString(), success =>
                                        {
                                            Debug.Log(success);
                                        },
                                                                error =>
                                        {
                                            Debug.Log(error);
                                        }
                                                                ));

                                        state = PractiseState.KinectDone;
                                    }
                                }

                                // Exit after first tracked body is found
                                break;
                            }
                        }
                    }

                    // Disable untracked body
                    for (int i = 0; i < frame.BodyFrameSource.BodyCount; i++)
                    {
                        if (!_bodies[i].IsTracked && bodyDrawer.Tracked)
                        {
                            //bodyDrawer.Untracked();
                        }
                    }

                    // Clear frame to get a new one
                    frame.Dispose();
                }
            }
        }
예제 #9
0
파일: Html.cs 프로젝트: yanfoe/YANFOE.v2
        /// <summary>
        /// Gets the specified download item.
        /// </summary>
        /// <param name="downloadItem">The download item.</param>
        public void Get(DownloadItem downloadItem)
        {
            Log.WriteToLog(LogSeverity.Info, downloadItem.ThreadID, string.Format("Processing HTML Started"), string.Format("{0}", downloadItem.Url));

            downloadItem.Result = new HtmlResult();

            downloadItem.Progress.Message = "Downloading " + downloadItem.Url;

            var cachePath = WebCache.GetPathFromUrl(downloadItem.Url, downloadItem.Section);

            Folders.CheckExists(cachePath, true);

            if (!downloadItem.IgnoreCache && !downloadItem.Url.Contains("YNoCache"))
            {
                if (File.Exists(cachePath + ".txt.gz"))
                {
                    Log.WriteToLog(LogSeverity.Info, downloadItem.ThreadID, string.Format(CultureInfo.CurrentCulture, "Url Found in Cache"), string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));
                    downloadItem.Result = new HtmlResult
                    {
                        Result  = Gzip.Decompress(cachePath + ".txt.gz"),
                        Success = true
                    };

                    return;
                }
            }

            if (InternalHandlers.Check(downloadItem))
            {
                InternalHandlers.Process(downloadItem, cachePath);
                return;
            }

            try
            {
                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Downloading"),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                downloadItem.Url = downloadItem.Url.Replace(" ", "+").Replace("%20", "+");

                var webClient = new WebClient
                {
                    Proxy = null
                };

                if (Settings.Get.Web.EnableProxy)
                {
                    webClient.Proxy = new WebProxy(Settings.Get.Web.ProxyUserName, (int)Settings.Get.Web.ProxyPort);
                }

                webClient.Headers.Add("user-agent", Settings.Get.Web.UserAgent);

                var encode = Encoding.GetEncoding(1252);

                foreach (var encoding in Settings.Get.Web.WebEncodings)
                {
                    if (downloadItem.Url.Contains(encoding.Key))
                    {
                        encode = encoding.Value;
                        break;
                    }
                }

                webClient.Encoding = encode;

                var outputString = webClient.DownloadString(downloadItem.Url);

                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Download Complete. Saving to Cache"),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                if (encode != Encoding.UTF8)
                {
                    var origBytes = encode.GetBytes(outputString);
                    var newBytes  = Encoding.Convert(encode, Encoding.UTF8, origBytes);
                    outputString = Encoding.UTF8.GetString(newBytes);
                    encode       = Encoding.UTF8;
                }

                File.WriteAllText(cachePath + ".txt.tmp", outputString, encode);

                Gzip.Compress(cachePath + ".txt.tmp", cachePath + ".txt.gz");
                File.Delete(cachePath + ".txt.tmp");

                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Process Complete."),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));


                downloadItem.Result.Result  = outputString;
                downloadItem.Result.Success = true;
                return;
            }
            catch (Exception ex)
            {
                Log.WriteToLog(LogSeverity.Error, LoggerName.GeneralLog, string.Format("Download Html {0}", downloadItem.Url), ex.Message);
                return;
            }
        }
 private Response CreateGzippedStringResponse(string contents, int statusCode = 200)
 {
     return(new Response(Gzip.Compress(Encoding.UTF8.GetBytes(contents)), (HttpStatusCode)statusCode));
 }
예제 #11
0
        /// <summary>
        /// 提交请求报文,获得响应报文
        /// </summary>
        /// <param name="Url">url地址</param>
        /// <param name="Context">请求报文</param>
        /// <returns></returns>
        private string RequestPost(string Url, string Context)
        {
            Stopwatch watcher = new Stopwatch();

            string PageStr = string.Empty;
            Uri    url     = new Uri(Url);

            byte[] reqbytes = Encoding.UTF8.GetBytes(Context);

            Stream          requestStream  = null;
            Stream          responseStream = null;
            StreamReader    srd            = null;
            HttpWebResponse resp           = null;

            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Timeout = 300000;   // 5分钟超时
                req.Method  = "POST";

                #region gzip
                if (checkGZip.Checked)
                {
                    using (MemoryStream gzipStream = Gzip.Compress(reqbytes))
                    {
                        reqbytes = gzipStream.ToArray();
                    }

                    req.Headers.Add("Content-Encoding", "gzip");
                }
                #endregion


                // 分别以xml、json、soap格式提交请求
                if (rbXML.Checked)
                {
                    req.Accept      = "application/xml, */*";
                    req.ContentType = "application/xml";
                }
                else if (rbJSON.Checked)
                {
                    req.Accept      = "application/json, */*";
                    req.ContentType = "application/json";
                }
                else if (rbSOAP.Checked)
                {
                    req.Accept      = "application/xml, */*";
                    req.ContentType = "text/xml";
                }

                req.ContentLength = reqbytes.Length;
                requestStream     = req.GetRequestStream();

                watcher.Start();

                requestStream.Write(reqbytes, 0, reqbytes.Length);
                requestStream.Close();

                resp = (HttpWebResponse)req.GetResponse();

                watcher.Stop();

                this.lblTimeConsuming.Text = "耗时:" + watcher.ElapsedMilliseconds.ToString() + "ms";

                responseStream = resp.GetResponseStream();

                if (resp.ContentEncoding.ToLower().Contains("gzip"))
                {
                    responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
                }

                srd      = new StreamReader(responseStream, Encoding.UTF8);
                PageStr += srd.ReadToEnd();

                // xml格式响应显示成良好的xml格式
                if (rbXML.Checked)
                {
                    PageStr = GetFormatXml(PageStr);
                }
                // soap格式响应结果换行显示
                if (rbSOAP.Checked)
                {
                    PageStr = PageStr.Replace(">\r\n", ">").Replace(">", ">\r\n");
                }
                //req.KeepAlive = false;
                responseStream.Close();
                srd.Close();
                resp.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }
                if (responseStream != null)
                {
                    responseStream.Close();
                }
                if (resp != null)
                {
                    resp.Close();
                }
                if (srd != null)
                {
                    srd.Close();
                }
            }
            return(PageStr);
        }
예제 #12
0
파일: Html.cs 프로젝트: Acrisius/YANFOE.v2
        /// <summary>
        /// Gets the specified download item.
        /// </summary>
        /// <param name="downloadItem">The download item.</param>
        public void Get(DownloadItem downloadItem)
        {
            Log.WriteToLog(LogSeverity.Info, downloadItem.ThreadID, string.Format("Processing HTML Started"), string.Format("{0}", downloadItem.Url));

            downloadItem.Result = new HtmlResult();

            downloadItem.Progress.Message = "Downloading " + downloadItem.Url;

            var cachePath = WebCache.GetPathFromUrl(downloadItem.Url, downloadItem.Section);

            Folders.CheckExists(cachePath, true);

            if (!downloadItem.IgnoreCache && !downloadItem.Url.Contains("YNoCache"))
            {
                if (File.Exists(cachePath + ".txt.gz"))
                {
                    Log.WriteToLog(LogSeverity.Info, downloadItem.ThreadID, string.Format(CultureInfo.CurrentCulture, "Url Found in Cache"), string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));
                    downloadItem.Result = new HtmlResult
                    {
                        Result  = Gzip.Decompress(cachePath + ".txt.gz"),
                        Success = true
                    };

                    return;
                }
            }

            try
            {
                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Downloading"),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                downloadItem.Url = downloadItem.Url.Replace(" ", "+").Replace("%20", "+");

                var webClient = new WebClient
                {
                    Proxy = null
                };

                webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

                var outputString = webClient.DownloadString(downloadItem.Url);

                var encode = Encoding.GetEncoding(1252);
                if (downloadItem.Url.IndexOf("ofdb", StringComparison.CurrentCultureIgnoreCase) != -1 || downloadItem.Url.Contains("sratim") || downloadItem.Url.Contains("filmweb") || downloadItem.Url.Contains("allocine"))
                {
                    encode = Encoding.GetEncoding("UTF-8");
                }

                if (downloadItem.Url.Contains("filmaffinity") || downloadItem.Url.Contains("filmdelta"))
                {
                    encode = Encoding.GetEncoding("ISO-8859-1");
                }

                if (downloadItem.Url.Contains("kinopoisk"))
                {
                    encode = Encoding.GetEncoding("windows-1251");
                }

                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Download Complete. Saving to Cache"),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                File.WriteAllText(cachePath + ".txt.tmp", outputString, encode);

                Gzip.Compress(cachePath + ".txt.tmp", cachePath + ".txt.gz");
                File.Delete(cachePath + ".txt.tmp");

                Log.WriteToLog(
                    LogSeverity.Info,
                    downloadItem.ThreadID,
                    string.Format(CultureInfo.CurrentCulture, "Process Complete."),
                    string.Format(CultureInfo.CurrentCulture, "{0}", downloadItem.Url));

                downloadItem.Result.Result  = outputString;
                downloadItem.Result.Success = true;
                return;
            }
            catch (Exception ex)
            {
                Log.WriteToLog(LogSeverity.Error, LoggerName.GeneralLog, string.Format("Download Html {0}", downloadItem.Url), ex.Message);
                return;
            }
        }