如何在Google Apps脚本中获取位置标题 [英] How to get the location header in Google Apps Script

查看:105
本文介绍了如何在Google Apps脚本中获取位置标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个Google Spreadsheet,其中:


  • 列A是应该301重定向的网址列表
  • >
  • 列B是我们预期的URL,将被重定向到

  • 列C是列A到达时的服务器响应代码

  • 列D是最终在点击列A时提供的URL

  • 列E是B和D匹配的布尔值



我在填充列D时遇到了问题。以下是我的尝试:

 函数getHeaders(tURL){
var response = UrlFetchApp.fetch(tURL);
var tHeaders = response.getAllHeaders();
}

我期待这个位置会出现在响应头中。但似乎这种方法只返回少数服务器响应头。我得到:X-Frame-Options,Date,P3P,Content-Length,Expires,X-XSS-Protection,Content-Encoding,Alternate-Protocol,Set-Cookie(array),Content-Type,Server,Cache-Control。

问题
有关另一种方式到达位置的建议吗?或者有关如何确定url被重定向到哪里的替代想法?

您需要使用 fetch(url,params)方法UrlFetchApp类并提供 followRedirects 参数设置为 false

  var response = UrlFetchApp.fetch(tURL,{'followRedirects':false}); 

followRedirects 默认为 true ,因此您的请求会返回它被重定向到的页面的标题和内容,而不是请求的页面。这就是为什么你没有看到位置标题。


I was experimenting with building a Google Spreadsheet where:

  • Column A is a list of URLs that should be 301 redirected
  • Column B was our intended URL to be redirect to
  • Column C is the server response code when hitting Column A
  • Column D is the URL that was ultimately served up when hitting Column A
  • Column E is a Boolean for B and D matching

I'm having problems with populating column D. Here is what I tried:

function getHeaders(tURL) {
   var response = UrlFetchApp.fetch(tURL);
   var tHeaders = response.getAllHeaders();
}

I was expecting that "location" would be in the response header. But it seems this method only returns a handful of server response headers. I get: X-Frame-Options, Date, P3P, Content-Length, Expires, X-XSS-Protection, Content-Encoding, Alternate-Protocol, Set-Cookie (array), Content-Type, Server, Cache-Control.

Question Any suggestions on another way to get to location? Or an alternate idea on how to determine where the url was redirected to?

解决方案

You need to use fetch(url, params) method of UrlFetchApp class and supply followRedirects parameter set to false:

var response = UrlFetchApp.fetch(tURL, {'followRedirects':false});

followRedirects defaults to true, and thus your request returns the headers and content of the page it was redirected to, not the requested page. That is why you do not see the Location header there.

这篇关于如何在Google Apps脚本中获取位置标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆