如何向 Google Apps Script Web App 发送有效的 HTTP 请求? [英] How to send a valid HTTP request to a Google Apps Script Web App?

查看:57
本文介绍了如何向 Google Apps Script Web App 发送有效的 HTTP 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们从 C++ 应用 (Arduino Sketck) 向 Google 应用脚本网络应用发送了 HTTP 请求,但我们得到了 HTTP 响应:HTTP/1.1 302 Moved Temporously.带有 http 请求的 url 可以在浏览器中正常工作.

We sent an HTTP request from a C++ app (Arduino Sketck) to a Google apps script web app, but we got the HTTP Response: HTTP/1.1 302 Moved Temporarily. The url with the http request works fine from a browser.

相同的代码也适用于其他网站,例如 www.google.com.不要使用 script.google.com.

The same code works also fine with other web site, like www.google.com. Do not work with script.google.com.

Google 应用程序脚本发布的网络应用程序是公开的,任何人甚至匿名都可以访问:

The Google apps script published web app is public, anyone even anonymous can access:

这里是我们使用的代码.

    client.println("GET /macros/s/AKfycbyQnmHekk4_NNy3Bl5ILzuSRkykMWaXQ7Rtojk7fFieDUbVqNM/exec?valore=7 HTTP/1.1");
    client.println("Host: script.google.com");
    client.println("Connection: close");
    client.println();

答案是:


    HTTP/1.1 301 Moved Permanently
    Content-Type: text/html; charset=UTF-8
    Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    Pragma: no-cache
    Expires: Mon, 01 Jan 1990 00:00:00 GMT
    Date: Wed, 03 Feb 2021 09:29:02 GMT
    Location: https://script.google.com/macros/s/AKfycbyQnmHekk4_NNy3Bl5ILzuSRkykMWaXQ7Rtojk7fFieDUbVqNM/exec?valore=7
    X-Content-Type-Options: nosniff
    X-Frame-Options: SAMEORIGIN
    Content-Security-Policy: frame-ancestors 'self'
    X-XSS-Protection: 1; mode=block
    Server: GSE
    Accept-Ranges: none
    Vary: Accept-Encoding
    Connection: close
    Transfer-Encoding: chunked
    11e
    <HTML>
    <HEAD>
    <TITLE>Moved Permanently</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
    <H1>Moved Permanently</H1>
    The document has moved <A HREF="https://script.google.com/macros/s/AKfycbyQnmHekk4_NNy3Bl5ILzuSRkykMWaXQ7Rtojk7fFieDUbVqNM/exec?valore=7">here</A>.
    </BODY>
    </HTML>
    0
    disconnecting from server.

网址正确(http://script.google.com/macros/s/AKkfycbyQnmHekk4_NNy3Bl5ILzuSRkykMWaXQ7Rtojk7fFieDUbVqNM/exec?valore=7"exec?valore=7) 但似乎谷歌应用程序脚本网络应用程序将请求重定向到相同的 url,使用 https 协议.

The url is correct ( http://script.google.com/macros/s/AKfycbyQnmHekk4_NNy3Bl5ILzuSRkykMWaXQ7Rtojk7fFieDUbVqNM/exec?valore=7) but seems that the google apps script web app redirect the request (to the same url, using the https protocol).

使用相同的代码,我们做了来自 Arduino 的其他 HTTP 请求,并且运行良好.例如我们做了:

Using the same code, we did others HTTP request from Arduino, and it worked fine. For example we did:

    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();

我们得到了响应`` HTTP/1.1 200 OK ```,html响应中包含了符合查询q=arduino的搜索结果

And we got the response `` HTTP/1.1 200 OK ```, and the html response contains the search result according with the query q=arduino

关于我们如何向 Google 应用脚本网络应用发送有效的 http/https 请求有什么建议吗?

Any suggestion on how we can send a valid http/https request to a Google apps script web app?

谢谢.

推荐答案

您已经注意到,Google 脚本应用程序正在将您从 HTTP 重定向到 HTTPS.某些 Google 站点可通过 HTTP 访问,如果他们不想,则不必重定向到 HTTPS.在您的示例中,http://www.google.com/search?q=arduino 确实重定向到 https://www.google.com/search?q=arduino&gws_rd=ssl.但是,您的客户端没有在请求中发送 User-Agent 标头,因此 Google 知道您的客户端不是浏览器,并且在您的情况下可能不会发出重定向.但在真正的浏览器中,确实如此.

As you have noticed, the Google script app is redirecting you from HTTP to HTTPS. Some Google sites are accessible via HTTP, they don't have to redirect to HTTPS if they don't want to. In your example, http://www.google.com/search?q=arduino does redirect, to https://www.google.com/search?q=arduino&gws_rd=ssl. But, your client is not sending a User-Agent header in the request, so Google knows your client is not a browser, and might not be issuing the redirect in your case. But in a real browser, it does.

输入网址 http://script.google.com/宏/s/AKfycbyQnmHekk4_NNy3Bl5ILzuSRkykMWaXQ7Rtojk7fFieDUbVqNM/exec?valore=7 进入浏览器确实重定向到https://script.google.com/macros/s/AKfycbyQnmHekk4_NNy3Bl5ILzuSRkykMWaXQ7Rtojk7fFieDUbVqNM/exec?valore=7.真正的浏览器会自动遵循该重定向,用户甚至可能不会注意到差异.

Putting the URL http://script.google.com/macros/s/AKfycbyQnmHekk4_NNy3Bl5ILzuSRkykMWaXQ7Rtojk7fFieDUbVqNM/exec?valore=7 into a browser does redirect to https://script.google.com/macros/s/AKfycbyQnmHekk4_NNy3Bl5ILzuSRkykMWaXQ7Rtojk7fFieDUbVqNM/exec?valore=7. A real browser will follow that redirect automatically, a user might not even notice the difference.

但是您的客户必须手动遵循重定向.这意味着从响应中提取 Location 标头,关闭现有连接(到端口 80 上的 script.google.com),连接到指定的服务器(script.google.com 端口 443),并在您最终发送 HTTP 请求之前启动与服务器的 SSL/TLS 加密会话.

But your client will have to follow the redirect manually. That means extracting the Location header from the response, closing the existing connection (to script.google.com on port 80), connecting to the specified server (script.google.com on port 443), and initiating an SSL/TLS encrypted session with the server before you can finally send the HTTP request.

SSL/TLS 很复杂,HTTP 有很多规则.不要尝试手动实现它们.您最好使用内置 HTTPS 支持的现有 HTTP 库.让它为您处理所有这些细节.

SSL/TLS is complex, and HTTP has a lot of rules to it. Don't try to implement them manually. You are best off using an existing HTTP library that has HTTPS support built in. Let it handle all of these details for you.

这篇关于如何向 Google Apps Script Web App 发送有效的 HTTP 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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