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

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. 我有一个Windows Mobile 6.5 palm设备(条形码读取器),并且我编写了一个小应用程序,它将读取条形码并将其存储在设备本地存储中的.xml文件中。 The same device needs to periodically retrieve some information from the server using an xml stream. 同一设备需要使用xml流定期从服务器检索某些信息。

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!!) http://www.pcreview.co.uk/threads/download-xml-files-via-webbrowser-control.2355816并引用此网页-> http://www.codeproject.com/csharp/HttpWebRequest_Response .asp (对不起,但是现在该页面似乎不可用,但是在我使用它的时候它是完全可读的!如果有人有快照,将不胜感激!)

The original code was written in C# and I've done some conversion work to translate it to vb.net language. 原始代码是用C#编写的,我已经做了一些转换工作以将其翻译为vb.net语言。 I used this site --> carlosag.net/Tools/CodeTranslator/Default.aspx. 我使用了这个站点-> 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!! 如果我在Visual Studio中以调试模式执行代码(请注意,调试阶段是在使用USB电缆将设备本身连接到开发PC的设备上进行的),一切似乎都运行良好! the .xml files is downloaded and stored as requested!! .xml文件已按要求下载并存储!

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. 我怀疑您没有从httprequest获得200“完成”状态。 There a number of reason for not getting the 200 done. 有很多原因导致无法完成200个任务。 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. 如果您不想使用嗅探器,请先检查请求中的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET Compact Framework-Windows Mobile 6.5上的CAB自动更新 - .NET Compact Framework - CAB auto update on Windows Mobile 6.5 Compact Framework中的XML文件下载 - XML file download in Compact framework 通过msi文件在Windows Mobile中部署.Net Compact Framework应用程序 - Deploying .Net Compact Framework applications in windows mobile via msi file .NET 紧凑型框架,枚举 windows (windows mobile) - .NET Compact Framework, enumerate windows (windows mobile) 在.NET紧凑框架中切换表单(windows mobile 6) - Switching forms in .NET compact framework (windows mobile 6) 如何使用C#Win Mobile 6.5 / Compact框架从图像创建缩略图? - How do I create a thumbnail from an image using C# Win Mobile 6.5/Compact framework? .NET Compact Framework-SQL Server Compact或平面文件作为备份 - .NET Compact Framework - SQL Server Compact or Flat File as backup 是否可以在Windows Mobile 6.5上运行为Windows Embedded Compact 7开发的程序? - Is it possible to run a program developed for Windows Embedded Compact 7 on Windows Mobile 6.5? 带有大字符串的JSON和.net Compact Framework(Windows Mobile) - JSON and .net Compact Framework (Windows Mobile) with large Strings C#.Net Compact Framework 3.5 FtpWebRequest Windows Mobile 6 - C# .Net Compact Framework 3.5 FtpWebRequest Windows Mobile 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM