suppress save/dialog in web browser and automate t

2020-05-03 08:41发布

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条回答
家丑人穷心不美
2楼-- · 2020-05-03 08:50

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.

查看更多
登录 后发表回答