private HttpWebRequest CreateRequest(RequestConfiguration configuration) { var request = (HttpWebRequest)WebRequest.Create(configuration.Url); request.UserAgent = configuration.UserAgent; request.Method = configuration.Method.ToString().ToUpper(); request.Accept = configuration.AcceptHeader; foreach (var header in configuration.Headers) { request.Headers.Set(header.Key, header.Value); } request.ContentType = configuration.ContentType; if (configuration.NoCache) { request.Headers.Set("Pragma", "no-cache"); } return(request); }
/// <summary> /// Execute a web request asynchronously /// </summary> /// <param name="configuration">Web request configuration</param> /// <param name="onComplete">On complete callback</param> /// <param name="onError">On error callback</param> public void Execute(RequestConfiguration configuration, Action <HttpWebResponse> onComplete, Action <Exception> onError) { if (configuration == null) { throw new ArgumentNullException("configuration"); } if (onComplete == null) { throw new ArgumentNullException("onComplete"); } if (onError == null) { throw new ArgumentNullException("onError"); } if (String.IsNullOrWhiteSpace(configuration.Url)) { throw new ArgumentException("Configuration must specify a URL", "configuration"); } var request = this.CreateRequest(configuration); if (String.IsNullOrEmpty(configuration.RequestBody) || !this.CanSendBody(configuration)) { // No body, just execute the request this.ExecuteRequestAsync(request, onComplete, onError); return; } // Body needs to be build async, otherwise the request will switch // to a blocking one for some stupid reason. // MSDN doesn't seem to expliclty say you need to wait for the body to complete // before executing the request, but that's what their sample code does, // so better safe than sorry :-) this.BuildRequestBody(request, configuration.RequestBody, r => this.ExecuteRequestAsync(r, onComplete, onError), onError); }
/// <summary> /// Initializes a new instance of the <see cref="HttpTester"/> class. /// </summary> /// <param name="iterations">Number of iterations to run</param> /// <param name="requestConfiguration">The request configuration to run</param> /// <param name="httpClient">The async http client to use</param> public HttpTester(int iterations, RequestConfiguration requestConfiguration, AsyncHttpClient httpClient) { this.iterations = iterations; this.requestConfiguration = requestConfiguration; this.httpClient = httpClient; }
protected void RequestConfiguration(string name, Action action) { this.currentRequestConfiguration = new RequestConfiguration() { Name = name }; this.requestConfigurations.Add(this.currentRequestConfiguration); action(); }
private bool CanSendBody(RequestConfiguration configuration) { return(configuration.Method != HttpMethod.Get && configuration.Method != HttpMethod.Head); }
private static HttpTesterResults RunConfig(RequestConfiguration config, int iterations) { var tester = new HttpTester(iterations, config, new AsyncHttpClient()); return tester.Execute(); }