2013-09-23 67 views
0
public static Task<string> GetData(string url, string data) 
    { 
     UriBuilder fullUri = new UriBuilder(url); 

     if (!string.IsNullOrEmpty(data)) 
      fullUri.Query = data; 

     WebClient client = new WebClient(); 

     var tcs = new TaskCompletionSource<string>(); 

     client.DownloadStringCompleted += (s, args) => 
     { 
      if (args.Error != null) 
       tcs.TrySetException(args.Error); // HERE 
      else if (args.Cancelled) 
       tcs.TrySetCanceled(); 
      else 
       tcs.TrySetResult(args.Result); 
     }; 

     client.DownloadStringAsync(fullUri.Uri); 

     return tcs.Task; 
    } 

地,上述方法在“//这里的”进入错误...TaskCompletionSource抛出错误,但不知道为什么

enter image description here

的事情是,我相信,网址和参数是正确的。除了创建我自己的API页面之外,我应该首先检查哪些典型的东西?

+0

它在'InnerException'中说:'远程服务器返回一个错误:NotFound。“所以,你走了,这是一个简单的404。 –

+0

该死的,我真的不明白为什么它是404,我已经直接从我知道的api复制代码。嗯,我会再试一次测试网址。 – Jimmyt1988

回答

0

首先,您可以使用WebClient.DownloadStringTaskAsync而不是包装自己的方法。

这就是说,例外情况仅仅是从WebClient返回的异常。 InnerException非常明确 - 服务器返回NotFound(即:404错误代码)。您设置异常的代码看起来完全有效。

相关问题