我目前有一个应用程序将数据发布到Web服务器,然后下载响应。我使用UploadDataAsync
来实现结果,使用以下内容:
using (var client = new WebClient())
{
var dataToPost = Encoding.Default.GetBytes(postBody);
client.Headers.Add(HttpRequestHeader.Authorization, authToken); // Add the authorization header
client.UploadDataCompleted += UploadCompleteCallback; // What do do when the download completes
client.UploadProgressChanged += UploadProgressCallback; // What to do when the download is in progress
client.UploadDataAsync(uri, "POST", dataToPost);
while (client.IsBusy)
{
// We're going to wait until the operation completes
}
}
它确实成功下载了该文件,但我想知道它是否正确,因为我没有使用DownloadDataAsync
。
我之所以不是因为我找不到使用DownloadDataAsync
方法发送POST数据的方法。
此外,UploadProgressCallback
仅在应用程序启动时调用一次,然后等待一段时间,并在文件完成时再次调用(因此不是流畅的下载进度指示器)。这可能是因为我使用了upload
代替download
?
对于完整性,这是我的UploadProgressCallback
:
private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
// Displays the operation identifier, and the transfer progress.
Console.WriteLine("{0} Downloaded {1} of {2} bytes. {3} % complete...",
(string)e.UserState,
e.BytesReceived,
e.TotalBytesToReceive,
e.ProgressPercentage);
}