suppress save/dialog in web browser and automate the download

旧时模样 提交于 2020-01-11 13:24:35

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/8597407/suppress-save-dialog-in-web-browser-and-automate-the-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!