WebClient无法下载AWS重定向内容

时间:2014-03-21 12:45:07

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

将以下网址粘贴到您的浏览器中,您可以下载包含航海河图的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是重定向,实际的root托管是AWS。看起来重定向的某些部分变得不稳定,我不确定如何纠正它。

2 个答案:

答案 0 :(得分:1)

如果可能的话,尝试使用新的HttpClient库,它可以像这样自动处理重定向:

HttpClientHandler handler = new HttpClientHandler { AllowAutoRedirect = true };

HttpClient client = new HttpClient(handler, true);

它还为您提供了比旧WebClient

更多的控制权

答案 1 :(得分:0)

你也可以这样:

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兼容。