2011-08-24 93 views
0

我正在编写一个应用程序,它需要将非常大的文件(通常超过150MB)下载到机器中。我知道WebClient具有缓冲限制,可以用于我的情况。因此,我遵循使用HttpWebRequest的方式在此处编写我的下载功能:http://dotnet.dzone.com/articles/2-things-you-should-consider?mz=27249-windowsphone7。以下是我的代码:在Windows Phone 7中下载大文件时抛出ProtocolViolationException

 private void _downloadBook(string _filePath) 
    { 
     Uri _fileUri = new Uri(_filePath); 
     //DownloadFileName = System.IO.Path.GetFileName(_fileUri.LocalPath); 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_fileUri); 
     request.AllowReadStreamBuffering = false; 
     request.BeginGetRequestStream(new AsyncCallback(GetData), request); 
    } 

    private void GetData(IAsyncResult result) 
    { 
     HttpWebRequest request = (HttpWebRequest)result.AsyncState; 
     HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result); 

     Stream str = response.GetResponseStream(); 

     byte[] data = new byte[16 * 1024]; 
     int read; 

     long totalValue = response.ContentLength; 
     while ((read = str.Read(data, 0, data.Length)) > 0) 
     { 
      if (streamToWriteTo.Length != 0) 
       Debug.WriteLine((int)((streamToWriteTo.Length * 100)/totalValue)); 

      streamToWriteTo.Write(data, 0, read); 
     } 
     streamToWriteTo.Close(); 
     Debug.WriteLine("COMPLETED"); 
    } 

然而,投掷ProtocolViolationException和以下堆栈:

System.Net.ProtocolViolationException了未处理 消息= ProtocolViolationException 堆栈跟踪: 在System.Net。 Browser.ClientHttpWebRequest.InternalBeginGetRequestStream(AsyncCallback callback,Object state) at System.Net.Browser.ClientHttpWebRequest.BeginGetRequestStream(AsyncCallback callback,Object state) at HHC_EbookReaderWP7.Co在System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi,Object obj,BindingFlags invokeAttr,Binder联编程序,Object参数,CultureInfo文化,布尔isBinderDefault,组件调用程序,布尔verifyAccess,StackCrawlMark & stackMark) 在System.Reflection.RuntimeMethodInfo.InternalInvoke(对象OBJ,的BindingFlags invokeAttr,粘结剂粘结剂,在System.Reflection.MethodBase.Invoke对象[]参数,CultureInfo的文化,StackCrawlMark & stackMark) (对象OBJ ,Object []参数) at System.Delegate.DynamicInvokeOne(Object [] args) at System.MulticastDelegate.DynamicInvokeImpl(Object [] args) 在System.Delegate.DynamicInvoke(对象[]参数) 在System.Windows.Threading.DispatcherOperation.Invoke() 在System.Windows.Threading.Dispatcher.Dispatch(的DispatcherPriority优先级) 在System.Windows.Threading.Dispatcher。 OnInvoke(Object context) at System.Windows.Hosting.CallbackCookie.Invoke(Object [] args) at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object [] args) at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate (IntPtr的pHandle,的Int32 nParamCount,ScriptParam [] pParams,ScriptParam & pResult)

什么错我的代码?还是我需要进一步呢?谢谢。

+0

准确的行和消息将有所帮助 – adontz

回答

0

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx

如前所述adontz。给我们引发异常的确切线。并根据silverlight文档。您需要调用begingetresponsestream而不是同步。的GetResponseStream。它还显示了协议违规漏洞的一些原因。检查这与WP7文档。

为了得到异常的确切路线,转到调试VS2010和GOTO例外的顶部菜单栏并启用“时抛出”

希望这有助于复选框。