2014-11-24 17 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()访问新位置。