2012-01-16 75 views
4

編輯:我很想在這個問題上放棄賞金 - 時間快到了 - 下面的所有評論都是最新的,但仍然沒有解決方案。使用WebClient進行wp7 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工作。另外,上面的代碼在控制檯應用程序中調用時也可以正常工作,Monodroid中也有相同的URL和類似的代碼。

最後,服務源非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] 
+0

什麼是異常的堆棧跟蹤? – 2012-01-16 20:15:16

+0

這是一種奇怪的行爲。該應用程序不會崩潰,但點擊按鈕時,不會顯示任何內容。當我遍歷代碼時,e.Result爲空,當我從clientOpenRead_Completed檢查'e.Result'時,它在'e.Result.ReadTimeout'中包含上述異常。我看到異常正在內部處理,但我仍然需要處理它,以便應用程序正常工作。 – IUnknown 2012-01-16 20:41:19

+0

你下載了什麼類型的數據?廣播流? XML? – vidalsasoon 2012-01-16 21:04:07

回答

1

嘗試調用OpenReadAsync()之前設置的webClientAllowReadStreamBufferingfalse

UPDATE

基於您的評論,我覺得你可能有參考System.Net.dll錯誤(非Windows phone)系統版本,這可能是有問題的問題的原因。在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 
+0

我最初認爲這樣做,但該財產似乎並沒有提供給我。 System.Net包含因爲WebClient已解決:錯誤1'System.Net.WebClient'不包含'AllowReadStreamBuffering'的定義,並且沒有接受類型'System.Net.WebClient'的第一個參數的擴展方法'AllowReadStreamBuffering'可能是發現(你是否缺少使用指令或程序集引用?) – IUnknown 2012-01-16 21:33:22

+0

當我在執行期間檢查webClient時,它確實顯示了AllowReadStreamBuffering屬性(值爲true),但無法在代碼中解析它。 – IUnknown 2012-01-16 21:35:19

+0

@IUnknown - WebClient的此屬性僅存在於Silverlight中。查看我的更新。 – Pol 2012-01-16 22:16:02

2

也許嘗試: webClient.DownloadStringAsync(yourUrl);代替

webClient.OpenReadAsync(yourUrl);

+0

試過 - e.Result仍然是空的。 – IUnknown 2012-01-16 21:42:47

+0

這對我有用。我有與上面所述相同的問題,並且我正在使用OpenReadAsync方法,現在使用DownloadStringAsync方法正常工作。謝謝 – 2013-04-14 05:28:13

-1

是否託管爲https?如果是的話,你可以嘗試從WP7的瀏覽器中點擊URL?

這個請求的數據量是多少?

+0

我正在測試的尺寸是63K,但我也在1K以下的測試 - 同樣的問題。好主意在WP7瀏覽器中測試,但不幸的是,當我試圖說它抱怨.json擴展名。我們對所有的json請求都使用它,我無法改變它。 – IUnknown 2012-01-20 15:24:03

1

您是否嘗試過使用HttpWebRequest做不同的ContentTypeAccept標題?

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

我還沒有嘗試過,但是當我檢查webClient對象時,請求,響應標頭和方法都設置正確。有沒有什麼具體的你試圖用上面的代碼來測試?謝謝 – IUnknown 2012-01-24 21:13:34

+0

一旦我遇到問題:請求返回了一個錯誤,但似乎都沒問題。解決方案是直接指定Url是絕對的。從有效的'http://'字符串中,它不會提取它。 – Ku6opr 2012-01-26 08:20:05