禁止在网络浏览器中保存/对话框并自动下载
我想自动下载客户端链接提示的 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;
}
}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚测试了一下,它会下载该文件。
您只需要添加用户代理,因为特定的 silverlight 下载取决于您运行的浏览器,因此如果它无法检测到,那么它将失败。
将用户代理更改为将触发您想要的适当下载的内容。
Just tested this out and it will download the file.
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.