开发者

Use WebClient, DownloadData method and Custom Header to Post to a web page

开发者 https://www.devze.com 2023-04-11 01:39 出处:网络
** Update I have a solution below, but it\'s rather manual... if there is an easier way, I\'d prefer that answer.

** 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://localho开发者_运维百科st: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.

//Using System.Diagnostics, set up a Stopwatch to time the execution
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

//convert your post data into a byte array... we're dealing with streams here
byte[] postData = Encoding.ASCII.GetBytes( postData );

//We're using the WebRequest object found in System.Net to do our work for us
WebRequest request = WebRequest.Create( url );

//Set all your headers
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.ContentLength = postData.Length;

//set up your request stream and write out your post data
Stream stream = request.GetRequestStream();
stream.Write( postData, 0, postData.Length );
stream.Close();

//Send the data and wait for a response. [blocking operation]
WebResponse response = request.GetResponse();

//Once you reach this line, the server has finished doing it's work, 
//and downloading has commenced
Console.WriteLine( "First Response: {0}", stopWatch.Elapsed );

//Start timing the download, wouldn't it be nice is there was a restart method? (.Net 4.0)
stopWatch.Reset();
stopWatch.Start();

//We want to receive the data. so ask for the response stream
Stream reponseStream = response.GetResponseStream();

//In my case, there are megabytes of information, so the console is no good,
//I'll store it in a file.
FileStream fileStream = File.Open( "result.txt", FileMode.OpenOrCreate );

//create a buffer to collect data as it is downloaded
byte[] buffer = new byte[1024];

//This loop will run as long as the responseStream can read data. (i.e. downloading data)
//Once it reads 0... the downloading has completed
int resultLength;
while( ( resultLength = reponseStream.Read( buffer, 0, buffer.Length ) ) != 0 )
{
    //You could further measure progress here... but I don't care.
    fileStream.Write( buffer, 0, resultLength );
}

//Everything is done... how long did it take to download?
Console.WriteLine( "Download Complete: {0}", stopWatch.Elapsed );

//housekeeping
fileStream.Flush();
fileStream.Close();
stopWatch.Stop();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号