예제 #1
0
        /// <summary>
        ///     Разбор HTTP-ответа службы
        /// </summary>
        private static Response ProcessResponse(HttpResult reply)
        {
            var response = new Response { StatusCode = reply.Code };

            var contentType = reply.ContentType;
            var ctFields = contentType.Split(';');
            var binary = ctFields[0] == "multipart/related";

            if (binary)
            {
                string boundary = null;
                for (var i = 1; i < ctFields.Length; i++)
                {
                    var field = ctFields[i].Trim(' ').Split('=');
                    if (field[0] == "boundary")
                    {
                        boundary = field[1].Trim('\"');
                        break;
                    }
                }

                if (boundary == null)
                {
                    throw new ApplicationException("Multipart boundary не найден в заголовке Content-Type");
                }

                var respData = reply.Content;
                var pos = 0;

                using (var stream = new MemoryStream(respData))
                {
                    using (var reader = new StreamReader(stream, Encoding.ASCII))
                    {
                        Func<string> readLine = () =>
                        {
                            // ReSharper disable once AccessToDisposedClosure
                            var line = reader.ReadLine();
                            if (line != null)
                            {
                                pos += line.Length + 2;
                            }
                            return line;
                        };

                        Action goToEmpty = () =>
                        {
                            string line;
                            do
                            {
                                line = readLine();
                            } while (!string.IsNullOrWhiteSpace(line));
                        };

                        goToEmpty();

                        var xmlString = readLine();
                        response.Xml.LoadXml(xmlString);

                        goToEmpty();
                    }
                }

                var length = respData.Length - (pos + boundary.Length + 6);
                var rData = new byte[length];
                Buffer.BlockCopy(respData, pos, rData, 0, length);
                response.Data = rData;
            }
            else
            {
                response.Xml.LoadXml(Encoding.UTF8.GetString(reply.Content));
            }

            return response;
        }
예제 #2
0
        /// <summary>
        ///     Анализ ошбок
        /// </summary>
        private static Reply ParseReply(HttpResult result)
        {
            var processedResponse = ProcessResponse(result);
            var reply = new Reply
            {
                Data = processedResponse.Data,
                ErrorCode = 0
            };

            if (processedResponse.StatusCode != HttpStatusCode.OK)
            {
                reply.ErrorCode = -1;

                var faultNode = processedResponse.Xml.GetNodeByLocalName("Fault");
                if (faultNode != null)
                {
                    var faultstringNode = faultNode.SelectSingleNode("faultstring");
                    var reason = faultstringNode != null ? faultstringNode.InnerText : "?";
                    var faultcodeNode = faultNode.SelectSingleNode("faultcode");
                    var code = faultcodeNode != null ? faultcodeNode.InnerText : "?";

                    reply.ErrorDescription = string.Format("FaultException. Code = {0}; Reason = {1};", code, reason);

                    var detailNode = faultNode.SelectSingleNode("detail");
                    if (detailNode != null)
                    {
                        reply.ErrorDescription += " Detail = " + detailNode.InnerText;
                    }
                }
                else
                {
                    reply.ErrorDescription = "Не удалось получить подробности ошибки";
                }
            }
            else
            {
                var responseNode = processedResponse.Xml.GetNodeByLocalName("return");
                reply.Xml = responseNode == null ? null : responseNode.InnerText;
            }

            return reply;
        }