コード例 #1
0
ファイル: Program.cs プロジェクト: Heyvaert/global
        /// <summary>
        /// does the actual http request.
        /// </summary>
        /// <param name="args">the list of parsed arguments.</param>
        private static void DoRequest(HttpArgs args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            //Console.WriteLine("--- BEGIN");
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            // display help if no arguments where provided or if arguments where invalid
            if (args == null || !AreArgsValid(args)) { HttpHelp(); return; }

            var http = new Global.Http.Http(args.Url);
            // for some reason if we set the value after it will not recognize it
            // we have to set before the other values
            if (args.KeepAlive != null) http.KeepAlive = (bool)args.KeepAlive;

            http = http.SetMethod(args.Method)
                .SetContentType(args.ContentType)
                .SetUserAgent(args.UserAgent)
                .SetPayload(args.Payload)
                .SetRequestEncoding(args.PayloadEncoding)
                .SetResponseEncoding(args.ResponseEncoding);
            if (args.Timeout != null) http.Timeout = (int)args.Timeout;
            if (args.Header != null && args.Header.Count > 0) http.Headers = args.Header;
            if (!string.IsNullOrEmpty(args.Accept)) http.Accept = args.Accept;
            if (args.Credential != null) http.Credentials = args.Credential;
            if (args.Proxy != null) http.Proxy = args.Proxy;
            // track how long the request took to show in the output console
            var started = DateTime.Now;
            var response = http.DoRequest();
            var ended = DateTime.Now;

            if (http.Response != null)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\n{0} {1} | {2} {3}", (int)http.Response.StatusCode, http.Response.StatusCode, args.Method, args.Url);

                if (http.Response.Headers != null)
                {
                    foreach (var h in http.Response.Headers)
                        Console.WriteLine("{0}: {1}", h.Key, string.Join(", ", h.Value));
                }
                Console.ForegroundColor = ConsoleColor.DarkGreen;

                if (http.Response.IsSuccessStatusCode)
                {
                    if (!string.IsNullOrEmpty(args.Regex))
                    {
                        var m = new Regex(args.Regex, RegexOptions.IgnoreCase | RegexOptions.Singleline).Matches(response);
                        if (m.Count > 0)
                        {
                            var builder = new StringBuilder("");
                            for (var i = 0; i < m.Count; i++)
                            {
                                builder.AppendFormat("Match {0}: {1}", i + 1, !string.IsNullOrEmpty(args.RegexGroup) ? m[i].Groups[args.RegexGroup] : m[i]);
                                builder.Append(i < m.Count - 1 ? "\n" : "");
                            }
                            response = builder.ToString();
                        }
                        else
                        {
                            response = "No matches were found!";
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("N/A (Failed unexpectadly).");
            }

            if (!string.IsNullOrEmpty(args.OutputPath) && !string.IsNullOrEmpty(response))
            {
                using (var writer = new StreamWriter(args.OutputPath)) writer.WriteLine(response);
            }
            else if (args.OutputToConsole && !string.IsNullOrEmpty(response))
            {
                Console.WriteLine("");
                Console.WriteLine(response);
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("");
            Console.WriteLine("{0}", new TimeSpan((ended - started).Ticks).ToString("c"));
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: persianyagami/global
        /// <summary>
        /// does the actual http request.
        /// </summary>
        /// <param name="args">the list of parsed arguments.</param>
        private static void DoRequest(HttpArgs args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            //Console.WriteLine("--- BEGIN");
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            // display help if no arguments where provided or if arguments where invalid
            if (args == null || !AreArgsValid(args))
            {
                HttpHelp(); return;
            }

            var http = new Global.Http.Http(args.Url);

            // for some reason if we set the value after it will not recognize it
            // we have to set before the other values
            if (args.KeepAlive != null)
            {
                http.KeepAlive = (bool)args.KeepAlive;
            }

            http = http.SetMethod(args.Method)
                   .SetContentType(args.ContentType)
                   .SetUserAgent(args.UserAgent)
                   .SetPayload(args.Payload)
                   .SetRequestEncoding(args.PayloadEncoding)
                   .SetResponseEncoding(args.ResponseEncoding);
            if (args.Timeout != null)
            {
                http.Timeout = (int)args.Timeout;
            }
            if (args.Header != null && args.Header.Count > 0)
            {
                http.Headers = args.Header;
            }
            if (!string.IsNullOrEmpty(args.Accept))
            {
                http.Accept = args.Accept;
            }
            if (args.Credential != null)
            {
                http.Credentials = args.Credential;
            }
            if (args.Proxy != null)
            {
                http.Proxy = args.Proxy;
            }
            // track how long the request took to show in the output console
            var started  = DateTime.Now;
            var response = http.DoRequest();
            var ended    = DateTime.Now;

            if (http.Response != null)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\n{0} {1} | {2} {3}", (int)http.Response.StatusCode, http.Response.StatusCode, args.Method, args.Url);

                if (http.Response.Headers != null)
                {
                    foreach (var h in http.Response.Headers)
                    {
                        Console.WriteLine("{0}: {1}", h.Key, string.Join(", ", h.Value));
                    }
                }
                Console.ForegroundColor = ConsoleColor.DarkGreen;

                if (http.Response.IsSuccessStatusCode)
                {
                    if (!string.IsNullOrEmpty(args.Regex))
                    {
                        var m = new Regex(args.Regex, RegexOptions.IgnoreCase | RegexOptions.Singleline).Matches(response);
                        if (m.Count > 0)
                        {
                            var builder = new StringBuilder("");
                            for (var i = 0; i < m.Count; i++)
                            {
                                builder.AppendFormat("Match {0}: {1}", i + 1, !string.IsNullOrEmpty(args.RegexGroup) ? m[i].Groups[args.RegexGroup] : m[i]);
                                builder.Append(i < m.Count - 1 ? "\n" : "");
                            }
                            response = builder.ToString();
                        }
                        else
                        {
                            response = "No matches were found!";
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("N/A (Failed unexpectadly).");
            }

            if (!string.IsNullOrEmpty(args.OutputPath) && !string.IsNullOrEmpty(response))
            {
                using (var writer = new StreamWriter(args.OutputPath)) writer.WriteLine(response);
            }
            else if (args.OutputToConsole && !string.IsNullOrEmpty(response))
            {
                Console.WriteLine("");
                Console.WriteLine(response);
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("");
            Console.WriteLine("{0}", new TimeSpan((ended - started).Ticks).ToString("c"));
        }