public WebRequest Build(Configuration configuration) { var request = WebRequest.Create(configuration.Uri); request.ContentType = configuration.ContentType; request.Method = configuration.Method; foreach (var header in configuration.Headers) request.Headers.Add(header.Key, header.Value); if (!string.IsNullOrEmpty(configuration.Body)) SetRequestBody(request, configuration.Body); return request; }
public string GetOutputFor(Configuration configuration) { var webRequestbuilder = new WebRequestBuilder(); var request = webRequestbuilder.Build(configuration); var output = string.Empty; var webResponse = request.GetResponse(); var dataStream = webResponse.GetResponseStream(); var streamReader = new StreamReader(dataStream); if (configuration.IncludeHeaders) output += GetHeaders(webResponse) + "\n\n"; output += streamReader.ReadToEnd(); return output; }
public Configuration GetConfiguration(string[] args) { var showHelp = false; var configuration = new Configuration(); var optionSet = new OptionSet() { { "h|help", "show this message and exit", option => showHelp = option != null }, { "u|uri=", "the uri", option => configuration.Uri = option }, { "t|content-type=", "the content type", option => configuration.ContentType = option }, { "m|method=", "the http method", option => configuration.Method = option }, { "o|output=", "the file to write output to", option => configuration.OutputFile = option }, { "i|include-headers", "include headers in output", option => configuration.IncludeHeaders = option != null }, { "b|body=", "the request body", option => configuration.Body = option }, { "a|add-header=", "add a new request header", option => configuration.AddHeader(option) }, }; try { optionSet.Parse(args); } catch { Console.WriteLine("Invalid usage."); ShowHelp(optionSet); return null; } if (showHelp || args.Length == 0 || !configuration.IsValid()) { ShowHelp(optionSet); return null; } return configuration; }