使用WebClient,DownloadData方法和自定义标题发布到网页

时间:2011-10-04 03:25:27

标签: c# post header webclient

**更新我在下面有一个解决方案,但它是相当手动的...如果有更简单的方法,我更喜欢这个答案。

我想记录网页的执行和下载时间,我正在尝试使用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一样。

1 个答案:

答案 0 :(得分:3)

好的......好吧我找到了一种使用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();