且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Google Apps脚本登录外部网站?

更新时间:2023-12-04 21:32:58

我从来没有能够将sessionDetails原样发送回站点.我一直在使用RegEx提取sessionDetails的相关部分并创建一个新的cookie字符串,然后将其发送回站点.要找出Cookie的哪些部分是相关的,请使用浏览器的网络日志(在开发人员工具中)检查您的浏览器为Cookie发布的内容,并将该字符串与sessionDetails进行比较.我在此处发布了一个示例.

I haven't ever been able to send sessionDetails back to the site as-is. I've been using RegEx to extract the relevant parts of sessionDetails and create a new cookie string, and send that back to the site. To find out what parts of the cookie are relevant, use your browser's network log (in the developer tools) to examine what your browser posts for the cookie, and compare that string to sessionDetails. I posted an example here.

var login = UrlFetchApp.fetch(url, options);
var sessionDetails = login.getAllHeaders()['Set-Cookie'];
Logger.log(sessionDetails); 
var cookie = sessionDetails.match(/Asp\.NetSessionId=[A-Z0-9]+;/)[0];  //modify this RegEx as needed

var response = UrlFetchApp.fetch(
  "https://example.com",
  {"headers" : {"Cookie" : cookie} }
);