簡體   English   中英

使用WebClient進行wp7 REST服務調用的超時異常

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

編輯:我很樂意在這個問題上放棄賞金 - 時間快到了 - 以下所有評論都是最新的,但仍無法解決。

得到一個奇怪的錯誤。 我已經將我的代碼縮減為絕對最簡單的形式,並且仍然會出現以下代碼的錯誤。

    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] 

也許試試:webClient.DownloadStringAsync(yourUrl);

代替

webClient.OpenReadAsync(yourUrl);

在調用OpenReadAsync()之前,嘗試將webClient AllowReadStreamBuffering設置為false

UPDATE

根據您的評論,我認為您可能有錯誤的(非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);

網址是否為https? 如果是這樣,你可以嘗試從WP7中的瀏覽器點擊URL嗎?

此請求的數據量是多少?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM