public static RunResult Run(string[] args) { var result = new RunResult(); var options = Parser.ParseArgs(args); HttpWebResponse response = null; var client = new Client(); try { response = client.GetResponse(options); if (options.CheckStatus) { result.ExitCode = GetExitStatus(response.StatusCode, options.AllowRedirects); } if (options.IsVerbose) { result.OutputMessage = Output.Write(options, client.Request, client.RequestBody); } var receivedStream = response.GetResponseStream(); if (receivedStream != null && receivedStream.CanRead) { var encode = Encoding.GetEncoding(!string.IsNullOrEmpty(response.ContentEncoding) ? response.ContentEncoding : "utf-8"); var readStream = new StreamReader(receivedStream, encode); result.ResponseBody = readStream.ReadToEnd(); readStream.Close(); } result.ResponseCode = (int)response.StatusCode; } catch (WebException web) { if (web.Response != null) { response = (HttpWebResponse)web.Response; result.ResponseCode = (int)response.StatusCode; result.ExitCode = GetExitStatus(response.StatusCode, options.AllowRedirects); result.ErrorMessage = web.Message; } else { result.ExitCode = Consts.EXIT.ERROR; result.ErrorMessage = web.Status + " " + web.Message; } } catch (Exception ex) { result.ExitCode = Consts.EXIT.ERROR; result.ErrorMessage = ex.Message; } result.OutputMessage += Output.Write(options, result, response); if (response != null) { response.Close(); } return(result); }
public static RunResult Run(string[] args) { var result = new RunResult(); var options = Parser.ParseArgs(args); HttpWebResponse response = null; var client = new Client(); try { response = client.GetResponse(options); if (options.CheckStatus) { result.ExitCode = GetExitStatus(response.StatusCode, options.AllowRedirects); } if (options.IsVerbose) { result.OutputMessage = Output.Write(options, client.Request, client.RequestBody); } var receivedStream = response.GetResponseStream(); if (receivedStream != null && receivedStream.CanRead) { var encode = Encoding.GetEncoding(!string.IsNullOrEmpty(response.ContentEncoding) ? response.ContentEncoding : "utf-8"); var readStream = new StreamReader(receivedStream, encode); result.ResponseBody = readStream.ReadToEnd(); readStream.Close(); } result.ResponseCode = (int)response.StatusCode; } catch (WebException web) { if (web.Response != null) { response = (HttpWebResponse)web.Response; result.ResponseCode = (int)response.StatusCode; result.ExitCode = GetExitStatus(response.StatusCode, options.AllowRedirects); result.ErrorMessage = web.Message; } else { result.ExitCode = Consts.EXIT.ERROR; result.ErrorMessage = web.Status + " " + web.Message; } } catch (Exception ex) { result.ExitCode = Consts.EXIT.ERROR; result.ErrorMessage = ex.Message; } result.OutputMessage += Output.Write(options, result, response); if (response != null) response.Close(); return result; }