** Update I have a solution below, but it’s rather manual… if there is an easier way, I’d prefer that answer.
I want to record execution and download time of a webpage, and I am trying to use the WebClient.DownloadDataAsync method to post data to a page and get a result.
I know there are numerous ways to post using WebClient or WebRequest objects, but none of them dispatch the DownloadProgressChanged event. This event is key, as I want to track progress of the download, which is in the area of 30 megs+.
I figure the best way to do this would be to construct the header manually, but I’m hitting a dead-ends. Mainly, I cannot add the data to the request.
WebClient client = new WebClient();
//!!!!*****no idea how to add the following data to the request
string data = "test1=value";
client.Headers.Add( "POST", "/About.aspx HTTP/1.1" );
client.Headers.Add( "Host", "http://localhost:12065" );
client.Headers.Add( "Content-Type", "application/x-www-form-urlencoded" );
client.Headers.Add( "Content-length", data.Length.ToString() );
client.DownloadProgressChanged += OnDownloadProgressChanged;
client.DownloadDataCompleted += OnDownloadDataCompleted;
client.DownloadDataAsync( new Uri( "http://localhost:12065/About.aspx" ) );
I am open to other solutions, or getting the header in this example to work as if the client is sending a POST.
OK… well I figured out a way to do this using WebRequest. It’s a little more complicated, but it does the trick. I am surprised that WebClient doesn’t do this already, but it seems as though WebClient has a number of unexpected issues.