/// <exception cref="InvalidDataException"></exception>
        private HttpResponse <string> ParseNextSubresponseFrom(PeekableStreamReader reader, string boundary)
        {
            var currentLine = reader.ReadFirstNonEmptyLine();

            if (currentLine != BatchSerializeHelper.GetOpenBoundaryString(boundary))
            {
                throw new InvalidDataException("Response boundary missed");
            }

            currentLine = reader.ReadFirstNonEmptyLineOrThrow("Content type header not found");
            //  if (currentLine?.Trim() != HttpHelper.HttpRequestContentTypeHeaderString)
            if (!currentLine.Trim().StartsWith(HttpHelper.ContentTypeHeader))
            {
                throw new InvalidDataException("Content type has invalid format");
            }

            currentLine = reader.ReadFirstNonEmptyLineOrThrow("Status code is missed");

            var resultCode = BatchSerializeHelper.GetResultCodeOrThrow(currentLine);



            var content = BatchSerializeHelper.ReadUntilBoundaryOrThrow(reader, boundary);

            return(new HttpResponse <string>(
                       new ResponseInfo((HttpStatusCode)resultCode),
                       content));
        }
Exemplo n.º 2
0
        public void WriteTo(Stream stream, Uri host)
        {
            foreach (var request in SubRequests)
            {
                var sb = new StringBuilder();
                sb.AppendLine(BatchSerializeHelper.GetOpenBoundaryString(_boundary));
                sb.AppendLine(HttpHelper.HttpRequestContentTypeHeaderString);
                sb.AppendLine();
                sb.AppendLine($"{request.Method.Name} {request.QueryAbsolutePath} {HttpHelper.Http11VersionCaption}");
                sb.AppendLine($"Host: {host.Authority}");
                stream.WriteUtf8(sb.ToString());

                if (request.Content != null)
                {
                    stream.WriteUtf8($"{HttpHelper.ContentTypeHeader}: {request.Content.ContentType}\r\n\r\n");
                    request.Content.WriteTo(stream, host);
                }
                else
                {
                    stream.WriteUtf8("\r\n");
                }
                stream.WriteUtf8("\r\n");
            }
            stream.WriteUtf8(BatchSerializeHelper.GetCloseBoundaryString(_boundary));
        }