简体   繁体   English

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

[英]Timeout exception with wp7 REST service call using WebClient

Edit: I would love to give away the bounty on this question - time is almost up - all comments below are up to date but still no resolution. 编辑:我很乐意在这个问题上放弃赏金 - 时间快到了 - 以下所有评论都是最新的,但仍无法解决。

Getting a strange error. 得到一个奇怪的错误。 I have reduced my code to the absolute simplest form and still getting error for the following code. 我已经将我的代码缩减为绝对最简单的形式,并且仍然会出现以下代码的错误。

    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(); sr.ReadToEnd(); always returns empty string and when I inspect 'e.Result' from clientOpenRead_Completed it contains the following exception: 总是返回空字符串,当我从clientOpenRead_Completed检查'e.Result'时,它包含以下异常:

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

Other important validations: The validURL works when requested from browser. 其他重要验证:validURL在浏览器请求时有效。 Also, the above code works fine when called in a Console app, also same URL and similar code works fine in Monodroid. 此外,上面的代码在控制台应用程序中调用时工作正常,同样的URL和类似的代码在Monodroid中工作正常。

Finally, the service source non-WCF. 最后,服务源非WCF。

Any ideas? 有任何想法吗?

Thanks. 谢谢。

EDIT: stacktrace at the point I am inspecting e.Result: (from a slightly different project but with the same problem) 编辑:堆栈跟踪在我正在检查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] 

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

instead of 代替

webClient.OpenReadAsync(yourUrl); webClient.OpenReadAsync(yourUrl);

Try setting AllowReadStreamBuffering of webClient to false before calling OpenReadAsync() . 在调用OpenReadAsync()之前,尝试将webClient AllowReadStreamBuffering设置为false

UPDATE UPDATE

Based on your comment, I think you may have wrong (non windows phone) version of System.Net.dll referenced and this may be reason of problem in question. 根据您的评论,我认为您可能有错误的(非Windows手机)版本的System.Net.dll引用,这可能是问题的原因。 On 7.1 (standard install) it should be 在7.1(标准安装)它应该是

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

In case you are on 7.0, it should be 如果你是7.0,它应该是

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

Have you tried to do the same with HttpWebRequest with different ContentType and Accept headers? 您是否尝试使用具有不同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);

Is the url hosted as https ? 网址是否为https? If so can you try hitting the URL from browser in the WP7 ? 如果是这样,你可以尝试从WP7中的浏览器点击URL吗?

What is the amount of data coming for this request ? 此请求的数据量是多少?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM