Google Apps Script 中的 Cookie 处理 - 如何在标头中发送 Cookie? [英] Cookie handling in Google Apps Script - How to send cookies in header?

查看:31
本文介绍了Google Apps Script 中的 Cookie 处理 - 如何在标头中发送 Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的脚本,用于从网页中获取文本并处理该字符串.但是,那个网站需要我登录.我成功登录了那个网站.我是这样登录的:

I'm trying to write a simple script that fetches text from a webpage and processes that string. But, that website requires me to be logged in. I was successful in logging in to that website. This is how I logged in:

var payload = {"name1":"val1","name2":val2"};

var opt ={"payload":payload,"method":"post"};

var respose = UrlFetchApp.fetch("http://website.com/login",opt);

登录后,该网站将我置于 http://website.com/home.我检查了 response.getContentText() 并且我可以确认我已成功登录,因为它包含来自 http://website.com/home 的文本.现在我需要获取 http://website.com/page 的内容并进行处理.我首先假设脚本可以自己处理 cookie 并继续

After logging in, the website places me in http://website.com/home. I checked response.getContentText() and I can confirm that I have been logged in successfully as it contains the text from http://website.com/home. Now I need to get the contents of http://website.com/page and process it. I first assumed the script can handle cookies by itself and proceeded with

var pagedata = UrlFetchApp.fetch("http://website.com/page);//Did not work

这显然不起作用,pagedata.getContentText() 说我先登录,这表明 cookie 没有成功传递..

That obviously didnt work and pagedata.getContentText() says me to login first, which indicates cookies were not successfully passed..

然后我尝试提取服务器在登录期间响应的 cookie 并将其与此请求一起发送.

I then tried to extract cookies which the server responded during login and to send it along with this request.

var cookie = response.getAllHeaders()['Set-Cookie'];     

// variable cookie now contains  a legitimate cookie.

// It contains 'JSESSIONID=blabla;Path=/' and 
// it is the ONLY cookie that server responds.

我试图在我的页面请求中发送那个 cookie.

I tried to send that cookie in my page request.

var header = {'Cookie':cookie};

var opt2 = {"header":header};

var pagedata = UrlFetchApp.fetch("http://website.com/page",opt2);

我认为即使是现在 cookie 也没有正确发送,因为内容再次提示我登录.

I think even now cookies were not properly sent, as the content again says me to login.

我是否正确传递 cookie?我需要有关在请求中发送 cookie 的正确方法的帮助.

Am I passing cookies correctly? I need help regarding the correct method of sending cookies in a request.

推荐答案

在这里您可以找到 cookie 规范:http://www.w3.org/Protocols/rfc2109/rfc2109

Here you can find cookies specification: http://www.w3.org/Protocols/rfc2109/rfc2109

您的代码中存在潜在问题:response.getAllHeaders()['Set-Cookie'] 可以返回一个字符串或一个字符串表,如果多个 'set-cookie' 属性从服务器发回.

You have a potential issue in your code: response.getAllHeaders()['Set-Cookie'] can return either a string or a table of string if multiple 'set-cookie' attributes are sent back from the server.

Eric 是对的,你不能不消化它就返回 cookie.

Eric is right, you cannot return the cookie without digesting it.

代码中的第二个错误:

var opt2 = {"header":header};

应该是

var opt2 = {"headers":header};

另请注意 GAS 使用 Google IP.可能发生两次连续获取使用不同 IP 的情况.您要连接的服务器可能依赖于会话 IP.

Be aware also that GAS uses Google IPs. It can happen that two consecutive fetch use different IPs. The server your are connecting to may be session-IP dependant.

您确定服务器在验证后只向您发送一个 cookie 吗?

Are you sure the server only send you back one cookie after an authentification ?

这篇关于Google Apps Script 中的 Cookie 处理 - 如何在标头中发送 Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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