谁可以使用 .net 3.5 下载此图像

Who can download this image using .net 3.5

斗争是真实的。不知何故我无法下载这张图片

string url = @"https://valgfrigave.dk/image/cache/catalog/200%20kr./Lyngby-glas-whiskey01_1000X1000px_200kr-750x750.jpg";

return错误是

 The remote server returned an error: (404) Not Found.

但是如果你 post url 进入浏览器,你会得到这些威士忌酒杯

我一直在使用 WebClient 和 HttpWebRequest。尝试了

的所有建议和答案

How do I programmatically save an image from a URL?

https://social.msdn.microsoft.com/Forums/en-US/d77b3c9a-d453-4622-8639-b7f0f91ecc9a/save-image-from-given-url?forum=csharpgeneral

https://forgetcode.com/CSharp/2052-Download-images-from-a-URL

https://www.codeproject.com/Articles/24920/C-Image-Download

https://social.msdn.microsoft.com/Forums/vstudio/en-US/c3289e1b-56aa-49b8-8c62-feaaf51cd0a2/download-images-from-url?forum=csharpgeneral

(...还有更多...)

但是其中 none 个能够下载图像。

编辑

下面是我尝试下载图像的代码。错误发生在 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;
            }
        } 
    }
}

第二次编辑

尝试建议的方法后仍然无效

解决方案

已找到解决方案,可以从@Stephan Schlecht 的评论中看到。 添加了以下方法并替换了 URI 创建:

添加的方法:

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;
}

替换我原来的代码:

    Uri innUri = null;
    Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri);

与:

    //Uri innUri = null;
    //Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out innUri);
    Uri innUri = MyUri(url);

问题

正如评论中已经怀疑的那样,问题是 URL 的一部分,即一个点,消失了。这似乎是 .NET 3.5 中的错误。

这里是 https 代理的屏幕截图:没有点是 404,因为缺少 URL 的这一部分:

.NET 3.5 的解决方案

由于您需要继续使用 .NET 3.5,因此您可以使用来自 SO 答案的这个很酷的解决方法:

在您的代码中添加上面 link 中的 MyUri 方法并像这样更改 URI 创建:

Uri innUri = MyUri(url);

然后下载成功!

结果