Пример #1
0
        public HttpsResponse MakeRequest(HttpsRequest request)
        {
            using (var writer = new NoCloseStreamWriter(tlsStream, Encoding.GetEncoding("ISO-8859-1"))) {
                writer.NewLine = "\r\n";
                writer.WriteLine(request.Method + " " + request.Path + " HTTP/1.0");

                writer.WriteLine("Host: " + host);

                if (request.PostData != null) {
                    if (request.ContentType == null)
                        throw new HttpException("HttpsRequest did not specifify a ContentType");

                    writer.WriteLine("Content-Type: " + request.ContentType);
                    writer.WriteLine("Content-Length: " + request.PostData.Length);
                }

                if (request.Authorization != null)
                    writer.WriteLine("Authorization: " + request.Authorization);

                writer.WriteLine();

                if (request.PostData != null) {
                    //writer.WriteLine();
                    writer.Flush();
                    tlsStream.Write(request.PostData, 0, request.PostData.Length);
                }
            }

            var response = new HttpsResponse();

            var ms = new MemoryStream();
            tlsStream.CopyToSafe(ms);
            ms.Position = 0;

            var statusLine = ms.ReadLineUTF8();
            var parts = statusLine.Split();
            if (parts.Length < 2 || !parts[0].StartsWith("HTTP"))
                throw new HttpException("Invalid HTTP response");

            int statusCode;
            if(!int.TryParse(parts[1], out statusCode))
                throw new HttpException("Invalid HTTP response");
            response.StatusCode = statusCode;

            while(true) {
                var line = ms.ReadLineUTF8();
                if (line.Length == 0)
                    break;

                var match = Regex.Match(line, "^([^:]+):\\s*(.*)$");
                if (match.Success)
                    response.AddHeader(match.Groups[1].Value, match.Groups[2].Value);
            }

            response.SetBody(ms);

            tlsStream.Dispose();

            return response;
        }
Пример #2
0
        public HttpsResponse MakeRequest(HttpsRequest request)
        {
            using (var writer = new NoCloseStreamWriter(tlsStream, Encoding.GetEncoding("ISO-8859-1"))) {
                writer.NewLine = "\r\n";
                writer.WriteLine(request.Method + " " + request.Path + " HTTP/1.0");

                writer.WriteLine("Host: " + host);

                if (request.PostData != null)
                {
                    if (request.ContentType == null)
                    {
                        throw new HttpException("HttpsRequest did not specifify a ContentType");
                    }

                    writer.WriteLine("Content-Type: " + request.ContentType);
                    writer.WriteLine("Content-Length: " + request.PostData.Length);
                }

                if (request.Authorization != null)
                {
                    writer.WriteLine("Authorization: " + request.Authorization);
                }

                writer.WriteLine();

                if (request.PostData != null)
                {
                    //writer.WriteLine();
                    writer.Flush();
                    tlsStream.Write(request.PostData, 0, request.PostData.Length);
                }
            }

            var response = new HttpsResponse();

            var ms = new MemoryStream();

            tlsStream.CopyToSafe(ms);
            ms.Position = 0;

            var statusLine = ms.ReadLineUTF8();
            var parts      = statusLine.Split();

            if (parts.Length < 2 || !parts[0].StartsWith("HTTP"))
            {
                throw new HttpException("Invalid HTTP response");
            }

            int statusCode;

            if (!int.TryParse(parts[1], out statusCode))
            {
                throw new HttpException("Invalid HTTP response");
            }
            response.StatusCode = statusCode;

            while (true)
            {
                var line = ms.ReadLineUTF8();
                if (line.Length == 0)
                {
                    break;
                }

                var match = Regex.Match(line, "^([^:]+):\\s*(.*)$");
                if (match.Success)
                {
                    response.AddHeader(match.Groups[1].Value, match.Groups[2].Value);
                }
            }

            response.SetBody(ms);

            tlsStream.Dispose();

            return(response);
        }