Download Files From Web Server

2019-08-18 06:44发布

问题:

I have the following response from a URL. How do I code to download the two files to my hard drive with the name = 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"
    }
  ]
}

My code that calls the URL is the following:

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

I am using json.net with visual basic 2008.

These are my classes

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

This is my code to deserializare json results

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

I want to read each id from the url response

For Each Data As FileAPI.RootObject In res

Next

I have the next error:

Expression is of type 'FillzAPI.FileAPI.RootObject', which is not a collection type.

How do I fix this error?

回答1:

A few points which will give you working code:

  • There is an easier way to download the JSON data.
  • You've accidentally declared RootObject.files as an array of lists, and its Get and Set methods are empty.
  • File is an unfortunate name for a class as it conflicts with System.IO.File.
  • It would be better to have the things in (what I named) FileData as properties. You can take advantage of auto-declared properties and not have to type the Get/Set methods.

Putting all those together, I arrived at

 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

Which outputs

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

and saves the files.