【问题标题】:WebClient fails to download AWS redirected contentWebClient 无法下载 AWS 重定向的内容
【发布时间】:2014-03-21 12:45:07
【问题描述】:

将以下网址粘贴到您的浏览器中,您可以下载一个 zip 文件,其中包含航海河图和沿河存在的一些桥梁示意图。

http://ienccloud.us/ienc/web/iencu37productsdownload.cfm?mode=0660&cell=U37AR126

但是,以下代码无法下载 zip 文件:

using (WebClient wc = new WebClient())
{
    wc.DownloadFile(@"http://ienccloud.us/ienc/web/iencu37productsdownload.cfm?mode=0660&cell=U37AR126", "download.zip");
}

而是下载以下纯文本 (html) 文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Domain Names, Web Hosting and Online Marketing Services | Network Solutions</title>
    <meta name="description" content="Find domain names, web hosting and online marketing for your website -- all in one place. Network Solutions helps businesses get online and grow online with domain name registration, web hosting and innovative online marketing services.">
    <meta name="keywords" content="domain names, domain registration, web hosting, website host, hosting services, online marketing, network solutions, networksolutions.com, netsol">

        <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"/>
    <link rel="meta" href="http://www.https//www.networksolutions.com/labels.rdf" type="application/rdf+xml" title="ICRA labels" />
    <meta http-equiv="pics-Label" content='(pics-1.1 "http://www.icra.org/pics/vocabularyv03/" l gen true for "http://https//www.networksolutions.com" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 1) gen true for "http://www.https//www.networksolutions.com" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 1))' />
    <meta name="robots" content="noodp, noydir">
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" media="screen" href="css/ns.css" />
    <script type="text/javascript" src="js/ns.js" ></script>
    <link rel="stylesheet" type="text/css" media="screen" href="css/template.css" />
</head>
<body>
<div id="wrapper" class="page">
<div id="content">
        <div class="container"></div>
<div class="container">
            <div class="box first lrg news">
                <div id="productDetails" class="newsMain">
                    <div class="tagline-container">
                        <div class="tagline">Welcome to Network Solutions</div>
                        <p><a href="http://www.networksolutions.com">Go to Network Solutions</a></p>
                    </div>
                </div>
                <div class="clr"></div>
            </div>
        </div>
    </div>
    </div>
</body>
</html>

我的理解是 URL 是一个重定向,实际的根托管是 AWS。看起来重定向的某些部分出现问题,我不知道如何纠正它。

【问题讨论】:

    标签: c# amazon-web-services http-redirect webclient


    【解决方案1】:

    如果可能,尝试使用新的 HttpClient 库,它可以自动为您处理重定向,如下所示:

    HttpClientHandler handler = new HttpClientHandler { AllowAutoRedirect = true };
    
    HttpClient client = new HttpClient(handler, true);
    

    它还为您提供了比旧 WebClient 更多的控制权

    【讨论】:

    • 这似乎很合适,但不幸的是我无法在这种情况下使用 .NET 4.5。
    • 很遗憾,你能在 WebClient 中读取标题吗?如果是这样,也许您可​​以自己处理重定向。
    【解决方案2】:

    你也可以这样得到:

    import requests
    url = 'http://ienccloud.us/ienc/web/iencu37productsdownload.cfm?mode=0660&cell=U37AR126'
    response = requests.get()
    # the binary zip data is now in response.content
    

    我不确定这是否与 .NET 4.5 兼容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 1970-01-01
      • 2020-11-29
      • 2012-04-17
      • 2021-02-09
      相关资源
      最近更新 更多