Making Web Request In C#

Last Edited: 2018-09-01 23:26:56

Yesterday I was playing around with .NET fiddle to do what I would think is the simplest thing you can do. In PHP, we can use file_get_contents(). In Linux we can use wget or cURL. We could use cURL in PHP with curl_init(). Python, it looks like you have to include a library, but still pretty simple. But in the Wide World of Windows, it seems to be a bit more difficult. I found that there's a lot of libraries that you can be using in your console program to get something from the web. Doing a search led me in about 4 different directions. I put this here mostly for my reference, but this seems to be the simplest way. Also, all you have to do for your starting point in a .NETfiddle would be to drop this in. Keep in mind that .NET fiddle has limitations. I wound up not even being able to load a 1 MB json file into it without running out of time.


using System;
using System.Net.Http;

public class Program {
public static void Main() {
using (var client = new HttpClient()) {
var req = client.GetAsync("https://www.freddythunder.com/php/returnTest.json").Result;
Console.WriteLine(req.Content.ReadAsStringAsync().Result);
}
Console.WriteLine("ends here");
}
}


This is important! Don't forget in the fiddle to add under NuGet Packages, System.Net.Http. If you search for http a ton of them show up and they all have different versions. Have fun.

I did at one point in time try to download and install VisualBasic back in I think 2008 when they started offering a smaller studio version for free. I remember falling asleep while it downloaded because it was so large. And then I tried installing it, took a really long time to get to a wall of errors I never resolved. Maybe some other time MS?
Comments

Categories

Recent Posts