使用WebClient, DownloadData方法和自定义头发送到网页

本文关键字:头发 网页 自定义 WebClient DownloadData 方法 使用 | 更新日期: 2023-09-27 18:10:25

**更新我有一个解决方案下面,但它是相当手动…如果有更简单的方法,我宁愿那个答案。

我想记录一个网页的执行和下载时间,我正在尝试使用WebClient。DownloadDataAsync方法,将数据发布到页面并获取结果。

我知道有很多方法来发布使用WebClient或WebRequest对象,但没有一个调度DownloadProgressChanged事件。这个事件是关键,因为我想要跟踪下载的进度,这是在30兆+区域。

我认为这样做的最好方法是手动构建头,但我遇到了一个死胡同。主要是我无法将数据添加到请求中。

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" ) );

我对其他解决方案持开放态度,或者在这个例子中让头工作,如果客户端正在发送POST。

使用WebClient, DownloadData方法和自定义头发送到网页

OK…好吧,我想出了一个方法来做到这一点使用WebRequest。这有点复杂,但它确实奏效了。我很惊讶WebClient还没有做到这一点,但似乎WebClient有许多意想不到的问题。

//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();