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

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

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下载取决于您运行的浏览器,因此如果它无法检测到,那么它将失败。

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