Downloading mod thumbnail using C# HttpClient

Place to get help with not working mods / modding interface.
Rikitav
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu Jul 04, 2024 4:51 pm
Contact:

Downloading mod thumbnail using C# HttpClient

Post by Rikitav »

I programming a small browser on C# to view and download mods using the internal Mod Portal API. Everything works gorgeous and the lists of mods are loaded, but when it comes to the assets, problems began. I use the most ordinary HttpClient for requests and when I try to request Thumbnail (let it be https://assets-mod.factorio.com/assets/ ... .thumb.png), the request is faulting with the exception "this host is unknown. (Assets-mod.Factorio.com:443)", although everything in the regular browser is calmly opening.
danbopes
Fast Inserter
Fast Inserter
Posts: 102
Joined: Mon Feb 07, 2022 9:29 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by danbopes »

"Host is unknown" indicates a DNS resolution failure. Ensure the link is correct (And no whitespace characters) and wherever the code is running ensure has proper Internet access.
Rikitav
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu Jul 04, 2024 4:51 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by Rikitav »

danbopes wrote: Sat Jun 07, 2025 4:57 pm "Host is unknown" indicates a DNS resolution failure. Ensure the link is correct (And no whitespace characters) and wherever the code is running ensure has proper Internet access.
I checked everything, the link is correct, there is Internet access, I even managed to download thumbnail ONCE, but after that it also gives an error. Maybe I need to indicate additional information to the request or are there any restrictions on access to this domain?
danbopes
Fast Inserter
Fast Inserter
Posts: 102
Joined: Mon Feb 07, 2022 9:29 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by danbopes »

Can you share some of the code (Like a minimal snippet), and I can try to help further.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15714
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by Rseding91 »

I don't have an answer to your issue (it should just work) - the internal game logic simply downloads the thumbnails and caches them in memory for the lifetime of the application.

However, on your project - there's no issue with what you're doing in general - but do be careful to not hand out your authentication token for people to use as that's against the TOS (bypassing the need to own the game) and will likely end with your account being permanently disabled.
If you want to get ahold of me I'm almost always on Discord.
eugenekay
Filter Inserter
Filter Inserter
Posts: 514
Joined: Tue May 15, 2018 2:14 am
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by eugenekay »

Rikitav wrote: Sat Jun 07, 2025 4:19 pmI use the most ordinary HttpClient for requests and when I try to request Thumbnail (let it be https://assets-mod.factorio.com/assets/ ... .thumb.png), the request is faulting with the exception "this host is unknown. (Assets-mod.Factorio.com:443)", although everything in the regular browser is calmly opening.
What is the Actual C# error message, and your calling code? This sounds like a DNS resolution failure; not yet getting to sending the HTTP request. Do you actually have “Assets-mod.Factorio.com” capitalized in your Code? All DNS labels should be normalized to lowercase before doing a Lookup; I don’t remember exactly how the various .Net SDK versions handle this case…
danbopes
Fast Inserter
Fast Inserter
Posts: 102
Joined: Mon Feb 07, 2022 9:29 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by danbopes »

I just wrote this dirt simple script to show that downloading assets works perfectly fine:

Code: Select all

using System.Text.RegularExpressions;

var client = new HttpClient();

var html = await client.GetStringAsync("https://mods.factorio.com/");

var matches = Regex.Matches(html, @"""(https://assets-mod\.factorio\.com/[^\""]+)\""");

foreach (Match match in matches)
{
    var url = match.Groups[1].Value;
    Console.WriteLine($"Downloading thumb: {url}");

    var fileName = Path.GetFileName(url);


    var stream = await client.GetStreamAsync(url);

    using var fs = File.OpenWrite(fileName);

    await stream.CopyToAsync(fs);
}
Downloaded all 20 images, and dumped them in the folder. You're doing something funky with the URL, or your DNS resolution is finicky. This is 100% on your end.
Rikitav
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu Jul 04, 2024 4:51 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by Rikitav »

eugenekay wrote: Sat Jun 07, 2025 11:33 pm Do you actually have “Assets-mod.Factorio.com” capitalized in your Code? All DNS labels should be normalized to lowercase before doing a Lookup;
About this... my english is very bad, so i used translator to write this message, its his fault, my request address is in lowercase.
Rikitav
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu Jul 04, 2024 4:51 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by Rikitav »

danbopes wrote: Sat Jun 07, 2025 11:14 pm Can you share some of the code (Like a minimal snippet), and I can try to help further.

Code: Select all

// "BitmapSource" is WPF's class
// "ModPageShortInfo" is class i use for deserialize mods 'short' info request

private const string AssetsUrl = "https://assets-mod.factorio.com";

public async Task<BitmapSource?> DownloadThumbnail(ModPageShortInfo modPage, CancellationToken cancellationToken = default)
{
    if (string.IsNullOrEmpty(modPage.Thumbnail))
	return null;
	
    try
    {
        await ThumbnailDownloadSemaphore.WaitAsync(cancellationToken);
        string thumbnailUrl = AssetsUrl + modPage.Thumbnail; // <--- CORRECT URL! CHECKED MANY TIMES!
        Debug.WriteLine("Requesting thumbnail : " + thumbnailUrl);

        using (HttpResponseMessage response = await httpClient.GetAsync(thumbnailUrl)) // This is where i getting an exception
        {
            response.EnsureSuccessStatusCode();
            using (Stream contentStream = await response.Content.ReadAsStreamAsync())
            {
                BitmapImage bitmapImage = new BitmapImage();
                
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = new MemoryStream();
                contentStream.CopyTo(bitmapImage.StreamSource);
                bitmapImage.EndInit();

                modPage.DownloadedThumbnail = bitmapImage;
            }
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Failed to load the image :", ex.Message);
    }

    ThumbnailDownloadSemaphore.Release();
    return modPage.DownloadedThumbnail;
}
Rikitav
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu Jul 04, 2024 4:51 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by Rikitav »

danbopes wrote: Sat Jun 07, 2025 11:34 pm I just wrote this dirt simple script to show that downloading assets works perfectly fine:
In fact, at some point the code began to work and the pictures were downloading, even a long amount of time and after many application restarts. This morning when I checked, the method stopped working again.
Rikitav
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu Jul 04, 2024 4:51 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by Rikitav »

eugenekay wrote: Sat Jun 07, 2025 11:33 pm What is the Actual C# error message

Code: Select all

System.Net.Http.HttpRequestException: "This host is unknown. (assets-mod.factorio.com:443)"
Just this
Rikitav
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu Jul 04, 2024 4:51 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by Rikitav »

Rseding91 wrote: Sat Jun 07, 2025 11:20 pm However, on your project - there's no issue with what you're doing in general - but do be careful to not hand out your authentication token for people to use as that's against the TOS (bypassing the need to own the game) and will likely end with your account being permanently disabled.
May they forgive me Wube, but I myself do not have a Factorio license, I play through a pirated version. The game in my region costs a lot of money, I just can't afford it at this age
danbopes
Fast Inserter
Fast Inserter
Posts: 102
Joined: Mon Feb 07, 2022 9:29 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by danbopes »

Rikitav wrote: Sun Jun 08, 2025 4:00 pm
danbopes wrote: Sat Jun 07, 2025 11:34 pm I just wrote this dirt simple script to show that downloading assets works perfectly fine:
In fact, at some point the code began to work and the pictures were downloading, even a long amount of time and after many application restarts. This morning when I checked, the method stopped working again.
You have a DNS issue, either in your router, or your ISP. I suggest using hardcoded public IP's for DNS resolution, like using google's DNS resolvers @ 8.8.8.8 and 8.8.4.4.

Could also be a VPN issue if you're using a VPN to access stuff.
Rikitav
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu Jul 04, 2024 4:51 pm
Contact:

Re: Downloading mod thumbnail using C# HttpClient

Post by Rikitav »

danbopes wrote: Sun Jun 08, 2025 7:03 pm You have a DNS issue, either in your router, or your ISP. I suggest using hardcoded public IP's for DNS resolution, like using google's DNS resolvers @ 8.8.8.8 and 8.8.4.4.

Could also be a VPN issue if you're using a VPN to access stuff.
alternate DNS resolver worked like a charm! TYSM!
Post Reply

Return to “Modding help”