Exemple #1
0
        /// <summary>
        /// При открытии потока для записи отправляем параметры для получения расписания экзаменов
        /// </summary>
        /// <param name="asynchronousResult"></param>
        private void GetRequestStreamExamsCallback(IAsyncResult asynchronousResult)
        {
            var request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            using (Stream postStream = request.EndGetRequestStream(asynchronousResult))
            {
                // Устанавливаем переменную с передаваемыми параметрами
                var contentParameters = new StringBuilder();
                contentParameters.Append("gr=").Append(_settingsSchedule.CurrentGroup);
                contentParameters.Append("&ss=").Append(_settingsSchedule.CurrentTerm);
                contentParameters.Append("&mode=").Append("Расписание экзаменов");

                string postData = contentParameters.ToString();

                // Convert the string into a byte array.
                byte[] byteArray = Encoding.GetEncoding(1251).GetBytes(postData);

                // Write to the request stream.
                postStream.Write(byteArray, 0, postData.Length);
            }

            // Start the asynchronous operation to get the response
            request.BeginGetResponse(GetResponseExamsCallback, request);
        }
Exemple #2
0
        /// <summary>
        /// Получаем ответ от сервера по заданным параметрам, обрабатываем данные (экзамены)
        /// </summary>
        /// <param name="asynchronousResult"></param>
        private void GetResponseExamsCallback(IAsyncResult asynchronousResult)
        {
            var request = (HttpWebRequest)asynchronousResult.AsyncState;
            var builder = new StringBuilder(100000);

            // End the operation
            try
            {
                using (var response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
                {
                    using (Stream data = response.GetResponseStream())
                    {
                        using (var reader = new BinaryReader(data))
                        {
                            var temp = new byte[1024];
                            int s;
                            while ((s = reader.Read(temp, 0, 1024)) != 0)
                            {
                                builder.Append(Encoding.GetEncoding(1251).GetString(temp, 0, s));
                            }
                        }
                    }
                }
            }
            catch
            {
                _errorMessage = Resources.Schedule.CannotConnectError;
            }


            string dataStr = builder.ToString();

            // Разбиваем полученную строку построчно
            _dataStrArrayExams.Clear();
            int position = 0;

            lock (_thisLock)
            {
                int res;
                while ((res = dataStr.IndexOf("\n", position, StringComparison.Ordinal)) != -1)
                {
                    _dataStrArrayExams.Add(dataStr.Substring(position, res - position));
                    position = res + 1;
                }
            }

            // Вызываем из потока парсинг данных и обновление полей
            Action action = ParseExams;

            _owner.BeginInvoke(action);

            Action <string> actionErrorMessage = NotifyPropertyChanged;

            _owner.BeginInvoke(actionErrorMessage, "ErrorMessage");
        }