Class HTTPResponse

HTTPResponse

该类允许用户访问有关 HTTP 响应的特定信息。

另请参阅

方法

方法返回类型简介
getAllHeaders()Object返回 HTTP 响应标头的属性/值映射,标头具有 以数组形式返回多个值。
getAs(contentType)Blob将此对象中的数据作为转换为指定内容类型的 blob 返回。
getBlob()Blob将此对象中的数据作为 blob 返回。
getContent()Byte[]获取 HTTP 响应的原始二进制内容。
getContentText()String获取编码为字符串的 HTTP 响应的内容。
getContentText(charset)String返回编码为给定字符集字符串的 HTTP 响应的内容。
getHeaders()Object返回 HTTP 响应标头的属性/值映射。
getResponseCode()Integer获取 HTTP 响应的 HTTP 状态代码(200 表示 OK 等)。

详细文档

getAllHeaders()

返回 HTTP 响应标头的属性/值映射,标头具有 以数组形式返回多个值。

// The code below logs the HTTP headers from the response
// received when fetching the Google home page.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getAllHeaders());

返回

Object - HTTP 标头的 JavaScript 键值对映射


getAs(contentType)

将此对象中的数据作为转换为指定内容类型的 blob 返回。本次 方法会为文件名添加相应的扩展名,例如“myfile.pdf”。不过, 最后一个句点之后的文件名部分(如果有)假定存在 扩展名。因此,“ShoppingList.12.25.2014”会变为 “ShoppingList.12.25.pdf”。

要查看转化次数的每日配额,请参阅 Google 配额 服务。新创建的 Google Workspace 网域可能会暂时适用更严格的条件 配额。

参数

名称类型说明
contentTypeString要转换为的 MIME 类型。对于大多数 blob,'application/pdf' 为 唯一有效的选项对于 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp''image/gif''image/jpeg''image/png' 中的任何一个也 有效。对于 Google 文档,'text/markdown' 也有效。

返回

Blob - 以 blob 形式表示的数据。


getBlob()

将此对象中的数据作为 blob 返回。

返回

Blob - 以 blob 形式表示的数据。


getContent()

获取 HTTP 响应的原始二进制内容。

// The code below logs the value of the first byte of the Google home page.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getContent()[0]);

返回

Byte[] - 以原始二进制数组形式的内容


getContentText()

获取编码为字符串的 HTTP 响应的内容。

// The code below logs the HTML code of the Google home page.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getContentText());

返回

String - HTTP 响应的内容,以字符串表示


getContentText(charset)

返回编码为给定字符集字符串的 HTTP 响应的内容。

// The code below logs the HTML code of the Google home page with the UTF-8 charset.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getContentText("UTF-8"));

参数

名称类型说明
charsetString一个字符串,表示用于对 HTTP 响应进行编码的字符集 内容

返回

String - HTTP 响应的内容,使用给定字符集编码


getHeaders()

返回 HTTP 响应标头的属性/值映射。

// The code below logs the HTTP headers from the response
// received when fetching the Google home page.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getHeaders());

返回

Object - HTTP 标头的 JavaScript 键值对映射


getResponseCode()

获取 HTTP 响应的 HTTP 状态代码(200 表示 OK 等)。

// The code below logs the HTTP status code from the response received
// when fetching the Google home page.
// It should be 200 if the request succeeded.
var response = UrlFetchApp.fetch("http://www.google.com/");
Logger.log(response.getResponseCode());

返回

Integer - HTTP 响应代码(例如 200 表示 OK)