In this article I will explain with an example, how to download JSON file from URL in C# and VB.Net.
In this Windows Forms application, the JSON file will be downloaded from the URL using WebClient class and then will be saved in a Folder (Directory) using C# and VB.Net.
JSON File URL
The following JSON file will be used in this article.
[
{
"CustomerId": 1,
"Name": "John Hammond",
"Country": "United States"
},
{
"CustomerId": 2,
"Name": "Mudassar Khan",
"Country": "India"
},
{
"CustomerId": 3,
"Name": "Suzanne Mathews",
"Country": "France"
},
{
"CustomerId": 4,
"Name": "Robert Schidner",
"Country": "Russia"
}
]
Form Design
Following is the Form.
Namespaces
You will need to import the following namespace.
C#
VB.Net
Downloading JSON File from URL
Inside the Form Load event handler, the JSON file is downloaded from the URL using DownloadFile method of the WebClient class.
DownloadFile method
It accepts the following two parameters:
address – The URL of the file to be downloaded.
fileName – Path of the Folder (Directory) where the file will be downloaded.
C#
protected void Form_Load(object sender, EventArgs e)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
WebClient webClient = new WebClient();
webClient.DownloadFile("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json", @"D:\Files\Customers.json");
}
VB.Net
Protected Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim webClient As WebClient = New WebClient()
webClient.DownloadFile("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json", "D:\Files\Customers.json")
End Sub
Screenshot
The downloaded JSON file
Downloads