Fetch a file from web using GLib/GIO from C

HTTP is one of the protocols supported by GIO, so you can open an HTTP URI just like any other file when using the GIO functions instead of standard C functions. Just use g_file_new_for_uri to create the file object and then you can read it just like a local file.

You can use g_file_read to get a GFileInputStream for the given URI and then g_data_input_stream_new to get a GDataInputStream for the input stream, which you can then use to read the file line-by-line. You have to upcast the GFileInputStream to a GInputStream before you can pass it to g_data_input_stream_new (or before you can do anything else useful with it), but if you're programming GTK in C, you're probably used to that by now.

Tags:

C

Url

Glib