簡體   English   中英

從服務器下載.xml文件並保存到Windows Mobile 6.5 .net Compact Framework中的本地存儲中

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

我有一個Windows Mobile 6.5 palm設備(條形碼讀取器),並且我編寫了一個小應用程序,它將讀取條形碼並將其存儲在設備本地存儲中的.xml文件中。 同一設備需要使用xml流定期從服務器檢索某些信息。

到處搜尋,我發現本文中提到了一個小圖書館:

http://www.pcreview.co.uk/threads/download-xml-files-via-webbrowser-control.2355816並引用此網頁-> http://www.codeproject.com/csharp/HttpWebRequest_Response .asp (對不起,但是現在該頁面似乎不可用,但是在我使用它的時候它是完全可讀的!如果有人有快照,將不勝感激!)

原始代碼是用C#編寫的,我已經做了一些轉換工作以將其翻譯為vb.net語言。 我使用了這個站點-> carlosag.net/Tools/CodeTranslator/Default.aspx。

結果如下:(您可能會用意大利語看到一些消息和警告)

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

現在,轉到我的問題的核心:

如果我在Visual Studio中以調試模式執行代碼(請注意,調試階段是在使用USB電纜將設備本身連接到開發PC的設備上進行的),一切似乎都運行良好! .xml文件已按要求下載並存儲!

但是,當我編譯該應用程序並將其部署到設備時,下載會失敗,但是不會出現任何錯誤(除了我在函數的和上放置的用於調試目的的顯式錯誤)。

似乎請求不是在等待服務器響應,並且如果我實時檢查服務器日志,則在設備顯示屏上顯示錯誤消息幾秒鍾后,我會看到請求到達服務器。

您必須使用諸如提琴手或鋼絲鉗之類的嗅探器來找出問題的根本原因。 我懷疑您沒有從httprequest獲得200“完成”狀態。 有很多原因導致無法完成200個任務。 您需要發送一些服務器的多個請求以獲得完整的響應。 您可能需要證書才能與服務器通信。 如果您不想使用嗅探器,請先檢查請求中的狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM