使用WebClient进行wp7 REST服务调用的超时异常

时间:2012-01-16 20:01:11

标签: c# web-services windows-phone-7 rest

编辑:我很乐意在这个问题上放弃赏金 - 时间快到了 - 以下所有评论都是最新的,但仍无法解决。

得到一个奇怪的错误。我已将代码缩减为绝对最简单的形式,但仍然会出现以下代码的错误。

    public partial class MainPage : PhoneApplicationPage {
    private readonly WebClient webClient;

    public MainPage() {
        InitializeComponent();

        webClient = new WebClient();
        webClient.OpenReadCompleted += clientOpenRead_Completed;
    }

    private void LoadButton_Click(object sender, RoutedEventArgs e) {
        webClient.OpenReadAsync(new Uri(@"validURL"));
    }

    private void clientOpenRead_Completed(object sender, System.Net.OpenReadCompletedEventArgs e) {
        using (var sr = new StreamReader(e.Result)) {
            Result.Text = sr.ReadToEnd();
        }
    }  
}

sr.ReadToEnd();总是返回空字符串,当我从clientOpenRead_Completed检查'e.Result'时,它包含以下异常:

base    {"Timeouts are not supported on this stream."}  System.SystemException {System.InvalidOperationException}

其他重要验证:validURL在浏览器请求时有效。此外,上面的代码在控制台应用程序中调用时工作正常,同样的URL和类似的代码在Monodroid中工作正常。

最后,服务源非WCF。

有什么想法吗?

感谢。

编辑:在我正在检查e.Result的时候进行堆栈跟踪:(来自一个略有不同的项目,但遇到同样的问题)

>   AppTest.dll!AppTest.Data.AsyncServiceProvider.clientOpenRead_Completed(object sender, System.Net.OpenReadCompletedEventArgs e) Line 20  C#
System.Net.dll!System.Net.WebClient.OnOpenReadCompleted(System.Net.OpenReadCompletedEventArgs e) + 0x15 bytes   
System.Net.dll!System.Net.WebClient.OpenReadOperationCompleted(object arg) + 0xc bytes  
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo rtmi, object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref System.Threading.StackCrawlMark stackMark)   
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x168 bytes 
mscorlib.dll!System.Reflection.MethodBase.Invoke(object obj, object[] parameters) + 0xa bytes   
mscorlib.dll!System.Delegate.DynamicInvokeOne(object[] args) + 0x98 bytes   
mscorlib.dll!System.MulticastDelegate.DynamicInvokeImpl(object[] args) + 0x8 bytes  
mscorlib.dll!System.Delegate.DynamicInvoke(object[] args) + 0x2 bytes   
System.Windows.dll!System.Windows.Threading.DispatcherOperation.Invoke() + 0xc bytes    
System.Windows.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority) + 0x83 bytes  
System.Windows.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context) + 0x8 bytes 
System.Windows.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args) + 0x19 bytes 
System.Windows.dll!System.Windows.Hosting.DelegateWrapper.InternalInvoke(object[] args) + 0x2 bytes 
System.Windows.RuntimeHost.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam[] pParams, ref System.Windows.Hosting.NativeMethods.ScriptParam pResult) + 0x5e bytes 
[External Code] 

4 个答案:

答案 0 :(得分:2)

也许试试: webClient.DownloadStringAsync(yourUrl);

而不是

webClient.OpenReadAsync(yourUrl);

答案 1 :(得分:1)

在致电webClient之前,尝试将false OpenReadAsync()设为System.Net.dll

更新

根据您的评论,我认为您可能有错误的(非Windows手机)版本的C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71\System.Net.dll 被引用,这可能是问题的原因。在7.1(标准安装)上它应该是

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone\System.Net.dll

如果您使用的是7.0,则应为

{{1}}

答案 2 :(得分:1)

您是否尝试使用HttpWebRequestContentType标题的Accept做同样的事情?

HttpWebRequest httpWebRequest = HttpWebRequest.CreateHttp(@"validURL");
httpWebRequest.Method = "GET";
httpWebRequest.BeginGetResponse((asyncresult) =>
{
    try
    {
        WebResponse webResponse = httpWebRequest.EndGetResponse(asyncresult);
        using (Stream stream = webResponse.GetResponseStream())
        {
            StreamReader Reader = new StreamReader(stream);
            string response = Reader.ReadToEnd();
        }
    }
    catch (Exception ex)
    {
        exception(ex);
    }
}, httpWebRequest);

答案 3 :(得分:-1)

网址是否为https?如果是这样,你可以尝试在WP7中点击浏览器中的URL吗?

此请求的数据量是多少?