从Web服务器下载文件

时间:2015-02-04 21:44:03

标签: vb.net httpwebrequest httpwebresponse

我从网址获得以下回复。如何编码将这两个文件下载到我的硬盘驱动器上,名称为id。

HTTP/1.1 200 OK
Content-Type: application/json

{
  "files": [
    {
      "format": "fillz-order-tab",
      "checksum": "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b",
      "acknowledged": false,
      "uri": "https://file-api.fillz.com/v1/orders/created/20140611T003336Z-8b975127",
      "date_created": "20140611T003336Z",
      "id": "20140611T003336Z-8b975127"
    },
    {
      "format": "fillz-order-tab",
      "checksum": "d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35",
      "acknowledged": false,
      "uri": "https://file-api.fillz.com/v1/orders/created/20140611T013545Z-3e2f2083",
      "date_created": "20140611T013545Z",
      "id": "20140611T013545Z-3e2f2083"
    }
  ]
}

我调用URL的代码如下:

Using response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
                    Dim reader As New StreamReader(response.GetResponseStream())
                    result = reader.ReadToEnd()

我正在使用json.net和visual basic 2008.

这些是我的课程

Public Class file
    Public format As String
    Public checksum As String
    Public acknowledged As String
    Public uri As String
    Public date_created As String
    Public id As String
End Class


Public Class RootObject
    Public Property files() As List(Of file)
        Get

        End Get
        Set(ByVal value As List(Of file))

        End Set

    End Property
End Class

这是我的deserializare json结果的代码

Dim res As RootObject = JsonConvert.DeserializeObject(Of FillzAPI.FileAPI.RootObject)(result)

我想从网址回复中读取每个ID

For Each Data As FileAPI.RootObject In res

Next

我有下一个错误:

Expression的类型为'FillzAPI.FileAPI.RootObject',它不是集合类型。

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

一些可以为您提供工作代码的要点:

  • 有一种更简单的方法来下载JSON数据。
  • 你不小心 将RootObject.files声明为列表,并将其Get和。{ Set方法是空的。
  • File是一个不幸的名字 它与System.IO.File冲突。
  • 最好有 (我命名的)FileData中的内容作为属性。你可以采取 自动声明的属性的优点,而不必键入 Get / Set方法。

把所有这些放在一起,我到了

 Option Infer On

Imports System.IO
Imports System.Net
Imports Newtonsoft.Json

Module Module1

    Public Class FileData
        Public Property format As String
        Public Property checksum As String
        Public Property acknowledged As String
        Public Property uri As String
        Public Property date_created As String
        Public Property id As String
    End Class

    Public Class RootObject
        Public Property Files As List(Of FileData)
    End Class

    Sub Main()
        ' I set this up on a local web server. Adjust as required.
        Dim src = "http://127.0.0.1/JsonSample.txt"

        ' An easy way to get a string from a web server...
        Dim wc As New WebClient
        'TODO: Try..Catch any error that wc.DownloadString throws.
        Dim jsonData = wc.DownloadString(src)

        'TODO: Try..Catch any error that JsonConvert.DeserializeObject throws.
        Dim y = JsonConvert.DeserializeObject(Of RootObject)(jsonData)

        ' Somewhere to save the downloaded files...
        Dim dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "My JSON test")
        If Not Directory.Exists(dest) Then
            Directory.CreateDirectory(dest)
        End If

        For Each x In y.Files
            Console.WriteLine(x.uri)
            'TODO: Try..Catch any error that wc.DownloadFile throws. Also perhaps use async methods.
            wc.DownloadFile(x.uri, Path.Combine(dest, x.id))
        Next

        Console.ReadLine()

    End Sub

End Module

哪个输出

  

https://file-api.fillz.com/v1/orders/created/20140611T003336Z-8b975127
  https://file-api.fillz.com/v1/orders/created/20140611T013545Z-3e2f2083

并保存文件。