Пример #1
0
        public void Process()
        {
            NetworkStream          stream   = null;
            kkmServiceResponseAtol respAtol = new kkmServiceResponseAtol();

            try
            {
                stream = client.GetStream();
                byte[] data = new byte[64]; // буфер для получаемых данных
                // получаем сообщение
                StringBuilder builder = new StringBuilder();
                int           bytes   = 0;
                do
                {
                    bytes = stream.Read(data, 0, data.Length);
                    builder.Append(Encoding.GetEncoding(1251).GetString(data, 0, bytes));
                }while (stream.DataAvailable);
                string             message    = builder.ToString();
                string             forceXml   = "";
                kkmServiceRequest  m_requests = Serializer.FromXml <kkmServiceRequest>(message);
                kkmServiceResponse m_response = SendInfoToKKM(m_requests, out forceXml);

                Console.WriteLine(message);
                message = "what a f?";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (client != null)
                {
                    client.Close();
                }
            }
        }
Пример #2
0
        public string GetPayment(string uuid, ref kkmServiceResponseAtol kkmResponseAtol)
        {
            Logs.LogAdd("**************************** НАЧАЛО ОТВЕТА НА ЗАПРОС ФИСКАЛЬНЫХ ДАННЫХ ****************************");
            if (timeToken.Hour == 20)
            {
                openChange = false;
                GetToken();
            }

            exeptionMessage = GetToken();
            if (!string.IsNullOrEmpty(exeptionMessage))
            {
                return(exeptionMessage);
            }

            try
            {
                string         url     = m_Config.url + "/possystem/v4/" + m_Config.codeGroup + "/report/" + uuid + "?token=" + HttpUtility.UrlEncode(token);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);


                request.Method            = "GET";
                request.ContentType       = "application/json; charset=utf-8";
                request.AllowAutoRedirect = true;
                request.KeepAlive         = true;
                request.Timeout           = 30000;

                WebResponse  resp       = request.GetResponse();
                Stream       dataStream = resp.GetResponseStream();
                StreamReader reader     = new StreamReader(dataStream, Encoding.UTF8);
                string       outp       = reader.ReadToEnd();

                using (var mm = new MemoryStream(Encoding.UTF8.GetBytes(outp)))
                {
                    DataContractJsonSerializer jsonFormatter = new DataContractJsonSerializer(typeof(kkmServiceResponseAtol));
                    kkmResponseAtol = (kkmServiceResponseAtol)jsonFormatter.ReadObject(mm);
                    Logs.LogAdd("Ответ от АТОЛА фискальный документ: " + kkmResponseAtol.fiscal_document_number);
                }
            }

            catch (WebException ex)
            {
                var temp = ex.Response as HttpWebResponse;
                Logs.LogAdd("ОШИБКА: " + ex.Message);
                string content;

                using (var r = new StreamReader(ex.Response.GetResponseStream()))
                    content = r.ReadToEnd();

                try
                {
                    using (var mm = new MemoryStream(Encoding.UTF8.GetBytes(content)))
                    {
                        using (StreamReader myStreamReader = new StreamReader(mm, Encoding.UTF8))
                        {
                            DataContractJsonSerializer jsonFormatter =
                                new DataContractJsonSerializer(typeof(DataJSONPayload));
                            var errorDataJSONPayload = (DataJSONPayload)jsonFormatter.ReadObject(myStreamReader.BaseStream);
                            if (errorDataJSONPayload.error != null)
                            {
                                exeptionMessage = errorDataJSONPayload.error.text;
                            }
                            Logs.LogAdd("Ошибка от АТОЛА : " + exeptionMessage);
                        }
                    }
                }
                catch (Exception)
                {
                }

                switch (temp.StatusCode)
                {
                case HttpStatusCode.Unauthorized:
                    Logs.LogAdd("ОШИБКА: " + content);
                    break;
                }
            }
            Logs.LogAdd("**************************** КОНЕЦ ОТВЕТА НА ЗАПРОС ФИСКАЛЬНЫХ ДАННЫХ ****************************");
            return(null);
        }