在网络浏览器中禁止保存/对话,并自动下载 [英] suppress save/dialog in web browser and automate the download

查看:140
本文介绍了在网络浏览器中禁止保存/对话,并自动下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to automate the download of an exe prompted from a link from the client side. I can get the first redirected link from http://go.microsoft.com/fwlink/?LinkID=149156 to http://www.microsoft.com/getsilverlight/handlers/getsilverlight.ashx. Please click and check how it works. fwlink -> .ashx - >.exe ...i want to get the direct link to the .exe. But the response returns 404 when requesting the Web handler through the code but if you try on Browser it actually downloads. Can anyone suggest how to automate the download form the above link? The code i am using to get the link redirected is this one.

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


推荐答案

测试了这个,它将下载文件。

Just tested this out and it will download the file.

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

You just needed to add the user-agent as the particular silverlight download depends on what browser you are running on, hence if it can't detect one then it will fail.

将用户代理更改为将触发您想要的适当下载的内容。

Change the user-agent to something that will trigger the appropriate download you want.

这篇关于在网络浏览器中禁止保存/对话,并自动下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆