开发者

WebClient.DownloadFile vs. WebClient.DownloadData

I am using WebClient.DownloadFile to download a small executable file from the internet. This method is working very well. However, I would now like to download this executable file into a byte array rather than onto my hard drive. I did some reading and came across the WebClient.DownloadData method. The problem that I am having with the downloadData method is that rather than downloading my file, my code is downloading the HTML data behind my file's download page.

I have tried using dozens of sites - each brings me the same issue. Below is the code I am using.

// Create a new instance of the System.Net 'WebClient'
        System.Net.WebClient client = new System.Net.WebClient();

        // Download URL
        Uri uri = new Uri("http://www35.multiupload.com:81/files/4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393开发者_JAVA技巧141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B/Project1.exe");

        byte[] dbytes = client.DownloadData(uri);

        MessageBox.Show(dbytes.Length.ToString()); // Not the size of my file

Keep in mind that I am attempting to download the data of an executable file into a byte array.

Thank you for any help, Evan


You are attempting to download a file using an expired token url. See below:

URL: http://www35.multiupload.com:81/files/4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B/Project1.exe`

Server: www35

Token: 4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B

You can't just download a file by waiting for the timer to end, and copy the direct link, it's a "token" link. It will only work for a specified period of time before redirecting you back to the download page (which is why you are getting HTML instead of binary data).

Workaround

You will have to download the multiupload's HTML and parse the direct download link from the HTML source code. Only this way provides a sure-fire way of getting an up-to-date token url.


How @Dark Slipstream said, you're attempting to download a file using an expired token url look how get the new url:

 System.Net.WebClient client = new System.Net.WebClient();

        // Download URL
        Uri uri = new Uri("http://www.multiupload.com/39QMACX7XS");

        byte[] dbytes = client.DownloadData(uri);
        string responseStr = System.Text.Encoding.ASCII.GetString(dbytes);
        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(responseStr);
        string urlToDownload = doc.DocumentNode.SelectNodes("//a[contains(@href,'files/')]")[0].Attributes["href"].Value;
        byte[] data = client.DownloadData(uri);
        length = data.Length; 

I dont parsing the exceptions

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜