none
WebClient对象不能多次调用么? RRS feed

  • 问题

  • 我实例化了一个WebClient对象,想获取string字符串,各种函数是这么写的。

     private void button1_Click(object sender, RoutedEventArgs e)
            {

    WebClient client = new WebClient();

     client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);

                client.DownloadStringAsync(new Uri("http://localhost:29809/server/contact.txt"), "contact");

            }

    第一次点击button的时候可以访问并获取字符串,但是再点一次,就没反应了,断电也跟不进去,请问这是为什么呢?

    2012年4月11日 12:54

答案

  • 你好,

    据我所知,WebClient是可以被多次调用的,下面是测试代码,作为参考:

     private void button1_Click(object sender, RoutedEventArgs e)
            {
                //set value as "initial value", after call client_DownloadStringCompleted, the value changed
               // textBlock1.Text = "initial value"; 
                WebClient client = new WebClient();
    
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
                client.DownloadStringAsync(new Uri("http://xurrency.com/api/usd/cny/100"), "contact");
            }
    
            void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            {
            }
    
            void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                string result = e.Result;
                textBlock1.Text = result;        
            }
    
            void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
            }

    如果你的问题仍然存在,请将重现这个问题的代码贴上来,大家一起研究。

    2012年4月12日 3:05
    版主

全部回复

  • 你好,

    据我所知,WebClient是可以被多次调用的,下面是测试代码,作为参考:

     private void button1_Click(object sender, RoutedEventArgs e)
            {
                //set value as "initial value", after call client_DownloadStringCompleted, the value changed
               // textBlock1.Text = "initial value"; 
                WebClient client = new WebClient();
    
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
                client.DownloadStringAsync(new Uri("http://xurrency.com/api/usd/cny/100"), "contact");
            }
    
            void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
            {
            }
    
            void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                string result = e.Result;
                textBlock1.Text = result;        
            }
    
            void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
            }

    如果你的问题仍然存在,请将重现这个问题的代码贴上来,大家一起研究。

    2012年4月12日 3:05
    版主
  • 不知道你的问题解决没,我在上传字符串的时候遇到了同样的问题,也是按下一次button可以发送,按第二次的时候就不发送了,提示是第一次的还没有传完,但是老长时间怎么会没有传完,不解了。
    2012年5月4日 0:23