title: download .xml file from server and save to local storage in windows mobile 6.5 .net compact framework
body: <p>I have a windows mobile 6.5 palm device (barcode reader), and I've written a small app that will read barcodes and stores it in .xml files on local storage of the device. The same device needs to periodically retrieve some information from the server using an xml stream.</p>
  
  <p>Googling around I've found a little library noted within this article:</p>
  
  <p><a href="http://www.pcreview.co.uk/threads/download-xml-files-via-webbrowser-control.2355816" rel="nofollow">http://www.pcreview.co.uk/threads/download-xml-files-via-webbrowser-control.2355816</a>
  and it refers to this web page --> <a href="http://www.codeproject.com/csharp/HttpWebRequest_Response.asp" rel="nofollow">http://www.codeproject.com/csharp/HttpWebRequest_Response.asp</a>
  (sorry but now the page seems to be unavailable but it was fully readable at the time i've used it!! if someone has a snapshot it will be appreciated!!)</p>
  
  <p>The original code was written in C# and I've done some conversion work to translate it to vb.net language. I used this site --> carlosag.net/Tools/CodeTranslator/Default.aspx.</p>
  
  <p>here is the result:
  (you may see some messages and warning in italian)</p>
  
  <pre><code>        '
  ' downloads a file from the specified url
  '
  ' params
  ' url - URL of the file to download
  ' destination - Full path of the destination of the file we are downloading
  ' the exit value of the sub is true if the file transfer is succesful
  Public Function DownloadFile(ByVal url As String, ByVal destination As String) As Boolean
      '
      ' function internal variables definition
      '
      Dim success As Boolean = False
      Dim request As System.Net.HttpWebRequest = Nothing
      Dim response As System.Net.WebResponse = Nothing
      Dim responseStream As System.IO.Stream = Nothing
      Dim fileStream As System.IO.FileStream = Nothing
      '
      ' operates the file download
      '
      MsgBox("Avvio Trasferimento", vbOKOnly, "Avviso:")
      Try
          '
          ' composes the web request
          '
          request = DirectCast(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
          request.Method = "GET"
          request.Timeout = 100000 ' timeout set to 100 seconds
          response = request.GetResponse()
          '
          ' gets response from the netowrk
          '
          responseStream = response.GetResponseStream()
          '
          ' directs the output to the destination file
          '
          fileStream = System.IO.File.Open(destination, _
                                           System.IO.FileMode.Create, _
                                           System.IO.FileAccess.Write, _
                                           System.IO.FileShare.None)
          '
          ' reads 10kb at a time
          '
          Dim maxRead As Integer = 10240
          Dim buffer As Byte() = New Byte(maxRead - 1) {}
          Dim bytesRead As Integer = 0
          Dim totalBytesRead As Integer = 0
          '
          ' loops until transfer is complete
          '
          While (InlineAssignHelper(bytesRead, responseStream.Read(buffer, 0, maxRead))) &gt; 0
              totalBytesRead += bytesRead
              fileStream.Write(buffer, 0, bytesRead)
          End While
          '
          ' no error were found, sets the retunr value to true (successful completion
          '
          success = True
      Catch exp As Exception
          '
          ' some errors are present, the file donwload as failed
          '
          success = False
          Debug.WriteLine(exp)
      Finally
          '
          ' closes all potential opened streams
          '
          If responseStream IsNot Nothing Then
              responseStream.Close()
          End If
          If response IsNot Nothing Then
              response.Close()
          End If
          If fileStream IsNot Nothing Then
              fileStream.Close()
          End If
      End Try
      '
      ' if part of the file was written and the transfer failed, delete the partial file
      '
      If Not success Then
          MsgBox("Trasferimento fallito - segnalare l'errore al supporto!", vbOKOnly, "Avviso:")
          If System.IO.File.Exists(destination) Then
              System.IO.File.Delete(destination)
          End If
      Else
          MsgBox("Trasferimento completato con successo!", vbOKOnly, "Avviso:")
      End If
      '
      ' sets the function return value
      '
      Return success
  End Function
  </code></pre>
  
  <p>Now, going to the core of my question:</p>
  
  <p>If I execute the code in debugging mode within visual studio (note the debbugging phase is operated with the device itself connected to the development pc using usb cable), all seems to run fine!! 
  the .xml files is downloaded and stored as requested!!</p>
  
  <p>However, when I compile the app and deploy it to the device, the download fails, but no errors are presented (except the explicit one I've placed at the and of the function for debugging purposes).</p>
  
  <p>It seems the request is not waiting for the server to respond, and if I check the server logs in realtime, I see the request reach the server some seconds after the error message is presented on device's display.</p>