使用WebClient的wp7 REST服务调用出现超时异常

本文关键字:超时 异常 调用 服务 WebClient wp7 REST 使用 | 更新日期: 2023-09-27 18:20:32

编辑:我很乐意透露这个问题的奖金-时间快到了-下面的所有评论都是最新的,但仍然没有解决方案。

得到一个奇怪的错误。我已经将我的代码简化为最简单的形式,但下面的代码仍然会出错。

    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。

有什么想法吗?

谢谢。

编辑:堆叠在我正在检查的点上。结果:(来自一个略有不同的项目,但有相同的问题)

>   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] 

使用WebClient的wp7 REST服务调用出现超时异常

也许可以尝试:webClient.DownloadStringAsync(yourUrl);

而不是

webClient.OpenReadAsync(yourUrl);

在调用OpenReadAsync()之前,请尝试将webClient的AllowReadStreamBuffering设置为false

更新

根据您的评论,我认为您可能引用了错误的(非windows手机)System.Net.dll版本,这可能是问题的原因。在7.1(标准安装)上,它应该是

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

如果你在7.0上,它应该是

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

您是否尝试过对具有不同ContentTypeAccept标头的HttpWebRequest执行相同操作?

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);

url是否托管为https?如果是这样的话,你可以尝试在WP7中从浏览器中点击URL吗?

此请求的数据量是多少?