google apps 脚本 ==> UrlFetchApp、方法 GET 和 cookie

use*_*272 8 cookies urlfetch google-apps-script

我使用 UrlFetchApp 发送用户和密码(POST 方法)。获取cookie后,并在其他请求中使用(GET方法)。但是这个新请求不起作用,我认为这个 cookie 在这个新请求中没有正确使用。谁能帮我?

  var opt ={
    "method":"post",
    "User-Agent" : "Mozilla/5.0",
    "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language" : "en-US,en;q=0.5",    
    "payload": this.payload.toString(), 
    "followRedirects" : false
  };
  var response = UrlFetchApp.fetch("https://edas.info/addTopic.php?c=19349",opt);
  var resp1=response.getContentText();    
  Logger.log(resp1);  
  response.getResponseCode();

  var headers = response.getAllHeaders();
  var cookies = headers['Set-Cookie']; 
  for (var i = 0; i < cookies.length; i++) {
    cookies[i] = cookies[i].split( ';' )[0];
  };


  opt = {
    "method" : "get",
    "User-Agent" : "Mozilla/5.0",
    "Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language" : "en-US,en;q=0.5",    
    "headers": {
      "Cookie": cookies.join(';')
    },
    "followRedirects" : false    
  };
  response = UrlFetchApp.fetch("https://edas.info/addTopic.php?c=19349", opt);
  var resp1=response.getContentText();  
  Logger.log(resp1);  
Run Code Online (Sandbox Code Playgroud)

小智 0

我无法为您的问题提供具体帮助,但有一个指针,如此处所示 Cookie Handling in Google Apps Script - How to send cookies in header?

正如/sf/users/100488531/所说:

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

您的代码是否在本地开发服务器上运行,并且仅在部署到 App Engine 后才会失败?还是本地也失败了?