Google apps script 什么Google AppsScript方法用于获取重定向的URL?

Google apps script 什么Google AppsScript方法用于获取重定向的URL?,google-apps-script,redirect,urlfetch,Google Apps Script,Redirect,Urlfetch,“www.mysite.com/mySecretKey1”重定向到“www.othersite.com/mySecretKey2” 在G.AppsScript中: var response = UrlFetchApp.fetch("https://www.mysite.com/mySecretKey1"); var headerString = response.getAllHeaders().toSource(); Logger.log(headerString); //str

“www.mysite.com/mySecretKey1”重定向到“www.othersite.com/mySecretKey2”

在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”)


更新:更一般地说,脚本如何从
响应中发现URL地址?

UrlFetchApp中有一个本机支持跟踪重定向。 您应该尝试设置:

followRedirects = true
在您提供给UrlFetchApp的选项中。 诸如此类:

var options = {
   "followRedirects" : true
 };
var result = UrlFetchApp.getRequest("http://your-url", options);
更新:更一般地说,脚本如何从响应中发现URL地址

与直觉相反,您需要禁用重定向,而不是禁用HttpException,如下所示:

var followedPost = UrlFetchApp.fetch(properUrl, {'followRedirects': false, 'muteHttpExceptions': false});
Logger.log(followedPost.getHeaders()['Location']);
.getHeaders()返回的对象将包含所请求资源的新位置。使用新的.fetch()访问该新位置。

在上详细说明了,以下是一个使用递归跟踪多个重定向的版本,只返回最终的规范URL:

function getRedirect(url) {
  var response = UrlFetchApp.fetch(url, {'followRedirects': false, 'muteHttpExceptions': false});
  var redirectUrl = response.getHeaders()['Location']; // undefined if no redirect, so...
  var responseCode = response.getResponseCode();
  if (redirectUrl) {                                   // ...if redirected...
    var nextRedirectUrl = getRedirect(redirectUrl);    // ...it calls itself recursively...
    Logger.log(url + " is redirecting to " + redirectUrl + ". (" + responseCode + ")");
    return nextRedirectUrl;
  }
  else {                                               // ...until it's not
    Logger.log(url + " is canonical. (" + responseCode + ")");
    return url;
  }
}  

function testGetRedirect() {
  Logger.log("Returned: " + getRedirect("http://wikipedia.org"));
}
此日志记录:

https://www.wikipedia.org/ is canonical. (200)
https://wikipedia.org/ is redirecting to https://www.wikipedia.org/. (301)
http://wikipedia.org is redirecting to https://wikipedia.org/. (301)
Returned: https://www.wikipedia.org/

这里有一个谷歌表单,你可以免费复制

它提供了跳转、代码和目的地,工作非常出色

为了以防万一(工作表/页面丢失),我将脚本编辑器中的代码粘贴到这里(这些代码都不是我的)

在H


它不仅可以让你发现问题(错误的代码),还可以通过将链接替换为最终目的地来提高链接的质量。

第一页或第二页的URL地址仍然不在
响应中。
。伙计,我爱你。这正是我想要的。
function redirectCheck(url, user, pwd) {
  try {
    function getResp(url, user, pwd){  
      var resp = UrlFetchApp.fetch(url, {
        muteHttpExceptions: true,
        followRedirects: false,
        headers: {
          'Authorization': 'Basic ' + Utilities.base64Encode(user+':'+pwd)
        }
      });
      return resp;
    }


var response = getResp(url, user, pwd);
var rCode = response.getResponseCode();
var redirectCount = 0;
var tCode = rCode.toString();
var location = url;
var domain = getDomain(url);

while (rCode == 301 || rCode == 302 && redirectCount <= 10) {
  redirectCount++;
  header = response.getHeaders();
  location = getFullUrl(header['Location'],domain);
  domain = getDomain(location);
  Logger.log('location: '+location);
  response = getResp(location, user, pwd);
  rCode = response.getResponseCode(); 
  tCode = tCode + " > " + rCode.toString();
  Utilities.sleep(500);// pause in the loop for 500 milliseconds
}     


Logger.log('redirectCount: '+redirectCount);
return tCode + "|" + redirectCount + "|" + location;


  } catch (error) {
    Logger.log(error);
    return "Error| |"+error;
  }
}
function getDomain(url) {
  var domain = '',
      protocol;
  if (url.indexOf("://") > -1) {
    domain = url.split('/')[2];
    protocol = url.split('/')[0];    
    //remove port number
    domain = domain.split(':')[0];
    //add protocol back
    domain = protocol+"//"+domain;
  }  

  return domain;
}

function getFullUrl(url,prevDom) {
  var fullUrl,
      domain = getDomain(url);
  if(domain == ''){
    fullUrl = prevDom+url;
  } else {
    fullUrl = url;
  }       

  return fullUrl;
}

function redirectCheckTest() {
  var test = redirectCheck('http://blog.pexcard.com/contractors/building-budget-construction-business/');
  Logger.log('test: '+test);
}
=IF(H11=200,"Not Redirected",IF(ISBLANK(C11),"",if(C11=J11,"Good","Bad")))
=IF(ISBLANK(B11),"",split(redirectCheck(B11,$L$5,$L$6),"|"))