コード例 #1
0
        public static HttpWebRequest InitializeWebRequest(ParsedHttpRequest parsed, Action <HttpWebRequest> callback = null)
        {
            var req = (HttpWebRequest)WebRequest.Create(parsed.Url);

            req.SetHttpHeaders(parsed.Headers);
            req.AddCookies(parsed);

            callback?.Invoke(req);

            req.SetRequestData(parsed.RequestBody);

            return(req);
        }
コード例 #2
0
        private static void AddCookies(this HttpWebRequest request, ParsedHttpRequest parsed)
        {
            if (parsed.CookieContainer != null)
            {
                request.CookieContainer = parsed.CookieContainer;
                return;
            }

            if (parsed.Cookies != null)
            {
                request.SetHttpCookies(parsed.Cookies, parsed.Uri);
            }
        }
コード例 #3
0
        public static ParsedHttpRequest ParseRawRequest(string raw, IgnoreHttpParserOptions options = null)
        {
            try
            {
                var lines = SplitLines(raw);

                var requestLine    = new RequestLine(lines);
                var requestHeaders = new RequestHeaders(lines);
                requestHeaders.AddHeader("Method", requestLine.Method);
                requestHeaders.AddHeader("HttpVersion", requestLine.HttpVersion);

                var requestCookies = new RequestCookies(lines);
                var requestBody    = new RequestBody(requestLine, lines);

                var parsed = new ParsedHttpRequest(options)
                {
                    Url         = requestLine.Url,
                    Uri         = new Uri(requestLine.Url),
                    Headers     = requestHeaders.Headers,
                    Cookies     = requestCookies.ParsedCookies,
                    RequestBody = requestBody.Body
                };

                parsed.ApplyIgnoreOptions();

                return(parsed);
            }
            catch (CouldNotParseHttpRequestException c)
            {
                Console.WriteLine($"Could not parse the raw request. {c.Message}");
                throw;
            }
            catch (Exception e)
            {
                Console.WriteLine($"Unhandled error parsing the raw request: {raw}\r\nError {e.Message}");
                throw;
            }
        }
コード例 #4
0
        public IHttpWebRequest BuildRequest(ParsedHttpRequest parsed)
        {
            var request = HttpWebRequestBuilder.InitializeWebRequest(parsed);

            return(new HttpWebRequestWrapper(request));
        }