The struggle is real. Somehow I can't download this image
string url = @"https://valgfrigave.dk/image/cache/catalog/200%20kr./Lyngby-glas-whiskey01_1000X1000px_200kr-750x750.jpg";
The return error is
The remote server returned an error: (404) Not Found.
But if you post the url into a browser, you'll get these whiskey glasses
I've been using WebClient and HttpWebRequest. Tried all of the suggestions an answers from
How do I programmatically save an image from a URL?
https://forgetcode.com/CSharp/2052-Download-images-from-a-URL
https://www.codeproject.com/Articles/24920/C-Image-Download
(...and alot more...)
But none of them is able to download the image.
EDIT
Below is the code where I try to download the image. The error occurs at client.OpenRead(innUri):
class Program
{
static void Main(string[] args)
{
string url = @"https://valgfrigave.dk/image/cache/catalog/200%20kr./Lyngby-glas-whiskey01_1000X1000px_200kr-750x750.jpg";
string fileFullPath = @"C:\TEMP\Image.jpg";
using (WebClient client = new WebClient())
{
Uri innUri = null;
Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri);
try
{
client.Headers.Add("Accept-Language", "en-US");
client.Headers.Add("Accept-Encoding", "gzip, deflate");
client.Headers.Add("Accept", " text/html, application/xhtml+xml, */*");
client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
using (Stream stream = client.OpenRead(innUri))
{
using (System.IO.FileStream output = new System.IO.FileStream(fileFullPath, FileMode.Create))
{
byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
}
}
catch (WebException we)
{
throw we;
}
}
}
}
2nd EDIT
Still doesn't work after trying the suggested approach
SOLUTION
A solution has been found and can be seen by the comment from @Stephan Schlecht. Added the following method and replaced the URI creation:
Method added:
public static Uri MyUri(string url)
{
Uri uri = new Uri(url);
System.Reflection.MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
System.Reflection.FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
if (getSyntax != null && flagsField != null)
{
foreach (string scheme in new[] { "http", "https" })
{
UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
if (parser != null)
{
int flagsValue = (int)flagsField.GetValue(parser);
// Clear the CanonicalizeAsFilePath attribute
if ((flagsValue & 0x1000000) != 0)
flagsField.SetValue(parser, flagsValue & ~0x1000000);
}
}
}
uri = new Uri(url);
return uri;
}
Replaced my original code:
Uri innUri = null;
Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri);
with:
//Uri innUri = null;
//Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri);
Uri innUri = MyUri(url);