繁体   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