日期:2014-05-18  浏览次数:20838 次

webClient.UploadData()的执行效率问题
写了个winform程序用来批量提交表单,每次循环都用下面一句上传数据

byte[] reponseData = webClient.UploadData(url, "POST", postData);

这句代码既提交了表单,也返回了服务器的响应

如果此时服务器比较忙,是不是就会卡在这里?

每次提交数据时都慢的要死,大牛们有什么好的意见?

需要使用多线程么?谢谢

只有30分了,另请问怎么多赚分?

------解决方案--------------------
是卡,你可以使用异步的 UploadDataAsync
------解决方案--------------------
有UploadDataCompleted 事件

C# code

public static void UploadDataInBackground2 (string address)
{
    WebClient client = new WebClient ();
    Uri uri = new Uri(address);
    string text = "Time = 12:00am temperature = 50";
    byte[] data = System.Text.Encoding.UTF8.GetBytes (text);
    string method = "POST";

    client.UploadDataCompleted += new UploadDataCompletedEventHandler (UploadDataCallback2);
    client.UploadDataAsync (uri, method, data);
}


public static void UploadDataCallback2 (Object sender, UploadDataCompletedEventArgs e)
{
    byte[] data = (byte[])e.Result;
    string reply = System.Text.Encoding.UTF8.GetString (data);

    Console.WriteLine (reply);
}