suppress save/dialog in web browser and automate the download
Asked Answered
U

1

1

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;
    }
}
Uptodate answered 21/12, 2011 at 22:46 Comment(4)
@DavidStratton: He's running C# code on the client (I assume); there are no security issues.Gwenore
Try adding cookies, or using GET rather than HEAD.Gwenore
Thanks SLaks.I tried the GET. It didn't work but for cookies ...i tried to add it to the response if that is what you mean?Uptodate
You are not writing a virus, are you? :) :) :)Crossfertilize
B
2

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.

Berton answered 22/12, 2011 at 3:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.