2017-05-15 78 views
0

使用Casperjs我從遠程服務器下載一些APK,但得到一個空的apk(0字節)。casperjs響應頭content-length 0

try { 
     this.echo("Attempting to download file "); 
     var fs = require('fs'); 
     var appUrl = 'http://website.com/download/someapp.apk'; 
     casper.download(appUrl, fs.workingDirectory+'/'+'newApk.apk'); 
    } catch (e) { 
     this.echo(e); 
    } 

而我正在接受這個響應標題。

Server: nginx/1.10.2 
Date: Mon, 15 May 2017 06:49:10 GMT 
Content-Length: 0 
Connection: keep-alive 
X-Robots-Tag: noindex 
Location:http://website.com/download/someapp.apk 
Expires: Thu, 01 Jan 1970 00:00:01 GMT 
Cache-Control: no-cache, max-age=0, no-cache, no-store, must- 
revalidate, proxy-revalidate, no-transform 
Pragma: no-cache 

回答

0

如果URL是正確的,並且您的代碼有效,它應該有效。爲了幫助你,這裏是一個最小的工作示例腳本,在當前目錄中下載圖像:

var casper = require('casper').create(); 

casper.start('https://duckduckgo.com', function() { 
    var url = 'https://duckduckgo.com/assets/logo_homepage.normal.v107.svg'; 
    this.download(url, 'duckduckgo-logo.svg'); 
}); 

casper.run(function() { 
    this.echo('Done.').exit(); 
}); 

如果仍然不能正常工作,它可能是一個問題與安全。在這種情況下,嘗試使用casperjs --web-security=no download.js運行腳本或配置您的卡斯帕例如像這樣:

var casper = require('casper').create({ 
    pageSettings: { 
    webSecurityEnabled: false 
    } 
}); 

OR

casper.page.settings.webSecurityEnabled = false;