0
votes

I'm writing an app which need to download very large size files (usually more than 150MB) into the machine. I knew that the WebClient has buffer limit and able to be used in my case. Therefore, I followed the way of using HttpWebRequest to write my download function in here: http://dotnet.dzone.com/articles/2-things-you-should-consider?mz=27249-windowsphone7. The following is my code:

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

However, it threw the ProtocolViolationException with the following stack:

System.Net.ProtocolViolationException was unhandled Message=ProtocolViolationException StackTrace: at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetRequestStream(AsyncCallback callback, Object state) at System.Net.Browser.ClientHttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state) at HHC_EbookReaderWP7.ComicPage._downloadBook(String _filePath) at HHC_EbookReaderWP7.ComicPage.b__2() at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Delegate.DynamicInvokeOne(Object[] args) at System.MulticastDelegate.DynamicInvokeImpl(Object[] args) at System.Delegate.DynamicInvoke(Object[] args) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority) at 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)

Anything wrong with my code? or do I need to further on it? Thanks.

1
Exact line and message will be helpfuladontz

1 Answers

0
votes

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

As adontz mentioned. Give us the exact line that throws the exception. And according the silverlight doc. you need to call begingetresponsestream instead of the sync. getresponsestream. It also shows you some reasons for a protocol violationexception. Check this with the WP7 documentation.

To get the exact line of the exception, goto Debug In the top menu bar of vs2010 and goto Exceptions and enable the checkboxes for "Thrown"

Hope this helps.