繁体   English   中英

在Web浏览器中禁止保存/对话框并自动下载

[英]suppress save/dialog in web browser and automate the download

我想从客户端的链接自动下载exe提示。 我可以从http://go.microsoft.com/fwlink/?LinkID=149156获取第一个重定向链接到http://www.microsoft.com/getsilverlight/handlers/getsilverlight.ashx 请单击并检查其工作原理。 fwlink - > .ashx - > .exe ...我想获得.exe的直接链接。 但是,当通过代码请求Web处理程序时,响应返回404,但如果您尝试使用Browser,则实际下载。 任何人都可以建议如何自动下载上述链接? 我用来获取链接重定向的代码就是这个。

public static string GetLink(string url)
{
    HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
    httpWebRequest.Method = "HEAD";
    httpWebRequest.AllowAutoRedirect = false;
   // httpWebRequest.ContentType = "application/octet-stream";
   //httpWebRequest.Headers.Add("content-disposition", "attachment; filename=Silverlight.exe");
    HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
    if (httpWebResponse.StatusCode == HttpStatusCode.Redirect)
    {
        return httpWebResponse.GetResponseHeader("Location");               
    }
    else
    {
        return null;
    }
}

刚刚测试了它,它将下载文件。

WebClient client = new WebClient();

client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

client.DownloadFile(url, "Filename.exe");

您只需要添加用户代理,因为特定的Silverlight下载取决于您运行的浏览器,因此如果它无法检测到,那么它将失败。

将用户代理更改为将触发所需的适当下载的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM