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

发布于 2024-12-22 07:17:48 字数 1154 浏览 6 评论 0原文

我想自动下载客户端链接提示的 exe。我可以从 http://go.microsoft.com/fwlink/?LinkID 获取第一个重定向链接=149156http://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;
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2024-12-29 07:17:48

刚刚测试了一下,它会下载该文件。

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

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

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");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文