【问题标题】:Use WebClient, DownloadData method and Custom Header to Post to a web page使用 WebClient、DownloadData 方法和自定义标头发布到网页
【发布时间】:2011-11-30 08:58:29
【问题描述】:

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

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

【问题讨论】:

  • 我是hitting dead-ends 是什么意思?什么样的,你的代码有什么问题?
  • 嗯...我无法将数据附加到请求中。

标签: c# post header webclient


【解决方案1】:

好的...好吧,我想出了一个使用 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2021-11-27
    • 2014-03-10
    • 1970-01-01
    • 2021-08-15
    相关资源
    最近更新 更多