简体   繁体   中英

download .xml file from server and save to local storage in windows mobile 6.5 .net compact framework

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.

Googling around I've found a little library noted within this article:

http://www.pcreview.co.uk/threads/download-xml-files-via-webbrowser-control.2355816 and it refers to this web page --> http://www.codeproject.com/csharp/HttpWebRequest_Response.asp (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!!)

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.

here is the result: (you may see some messages and warning in italian)

        '
' 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))) > 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

Now, going to the core of my question:

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!!

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).

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.

You have to use a sniffer like fiddler or wireshark to find root cause of issue. I suspect that you aren't getting the 200 'done' status from the httprequest. There a number of reason for not getting the 200 done. Some server you need to send multiple request to get the full response. You may need a certificate to communicate with the server. If you don't want to use a sniffer check the status from the request as a start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM