2014-11-24 15 views
2

'www.mysite.com/mySecretKey1' 重定向到 'www.othersite.com/mySecretKey2'Google AppsScript方法用於獲取重定向的URL?

在G.AppsScript:

var response = UrlFetchApp.fetch("https://www.mysite.com/mySecretKey1"); 
    var headerString = response.getAllHeaders().toSource(); 
    Logger.log(headerString); 
    //string 'www.othersite.com.my/SecretKey2' is not present in log. 

如何將腳本發現它被重定向的URL地址到(即字符串'www.othersite.com/mySecretKey2')?

更新:更一般地,腳本如何從response發現URL地址?

回答

2

UrlFetchApp中有一個本機支持來跟蹤重定向。 你應該嘗試設置:

followRedirects = true 

在你提供給UrlFetchApp的選項。 類似的東西:

var options = { 
    "followRedirects" : true 
}; 
var result = UrlFetchApp.getRequest("http://your-url", options); 
+2

「響應」中仍然沒有第一頁或第二頁的URL地址。 – user3645994 2014-11-25 05:29:21

0

更新:更一般地,怎麼會在腳本的反應中發現的URL地址?

直覺相反,你需要禁用重定向靜音HttpExceptions,就像這樣:

var followedPost = UrlFetchApp.fetch(properUrl, {'followRedirects': false, 'muteHttpExceptions': false}); 
Logger.log(followedPost.getHeaders()['Location']); 

由.getHeaders返回(對象)將包含被請求的資源的新位置。使用新的.fetch()訪問新位置。