本文介绍了从Web服务器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从URL得到以下响应.我该如何编码将两个文件下载到名称为id的硬盘中.
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"
}
]
}
我调用URL的代码如下:
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()
我在Visual Basic 2008中使用json.net.
I am using json.net with 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
这是我反序列化json结果的代码
This is my code to deserializare json results
Dim res As RootObject = JsonConvert.DeserializeObject(Of FillzAPI.FileAPI.RootObject)(result)
我想从网址响应中读取每个ID
I want to read each id from the url response
For Each Data As FileAPI.RootObject In res
Next
下一个错误:
表达式的类型为'FillzAPI.FileAPI.RootObject',这不是集合类型.
Expression is of type 'FillzAPI.FileAPI.RootObject', which is not a collection type.
如何解决此错误?
推荐答案
一些可以为您工作的代码:
A few points which will give you working code:
- 有一种下载JSON数据的简便方法.
- 你不小心将
RootObject.files
声明为列表数组,并将其Get
和Set
方法为空. -
File
是类的不幸名称,因为它与System.IO.File
冲突. - 最好有(我命名为)
FileData
中的内容作为属性.你可以采取自动声明的属性的优势,而不必键入Get
/Set
方法.
- There is an easier way to download the JSON data.
- You've accidentallydeclared
RootObject.files
as an array of lists, and itsGet
andSet
methods are empty. File
is an unfortunate name for a class asit conflicts withSystem.IO.File
.- It would be better to have thethings in (what I named)
FileData
as properties. You can takeadvantage of auto-declared properties and not have to type theGet
/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
哪个输出
并保存文件.
这篇关于从Web服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!