如何使用App Engine网址抓取服务处理Cookie,例如网络浏览器? [英] How to handle cookies like a web browser using App Engine url fetch service?

查看:133
本文介绍了如何使用App Engine网址抓取服务处理Cookie,例如网络浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到许多关于在App Engine上使用Cookie进行网址提取的问题,但许多人不正确地做了这件事,并且没有在Java App Engine上确认为正在运行的解决方案。



我想要做的是:


  1. 发出一个请求


  2. 使用这些cookie发出第二个请求。

它有多难be



在开发服务器上这很容易实现,因为开发服务器会自动在请求之间复制cookie - 实际上不可能阻止它执行此操作。但真正的App Engine urlfetch服务并不会这样做。

使用

b
$ b

  new HTTPRequest(url,HTTPMethod.GET,fetchOptions.doNotFollowRedirects())


来创建请求对象。



doNotFollowRedirects()出于某种原因至关重要。我不明白为什么。

然后从响应中获取set-cookie标题(名称 equalsIgnoreCase(Set-Cookie)) )并创建一个对应的 Cookie 标题 - 修剪每个Set-Cookie标题中分号后的所有内容(如果有的话),然后连接所有这些cookie与; 一起作为分隔符。

这不正确地处理过期等,但是这个应该足够用于大多数目的。


I have seen many questions about using cookies with url fetch on App Engine, but many people doing it incorrectly, and no confirmed-as-working solutions on Java App Engine.

All I want to do is:

  1. Make one request
  2. Get the cookies from that request.
  3. Make a second request, with those cookies.

How hard can it be??

This is easy to do on the dev server, because the dev server copies cookies across requests automatically - and indeed it's impossible to stop it doing this. But the real App Engine urlfetch service does not do this.

解决方案

Use

new HTTPRequest(url, HTTPMethod.GET, fetchOptions.doNotFollowRedirects())

to create the request object.

The doNotFollowRedirects() is essential for some reason. I don't understand why.

Then get the set-cookie headers from the response (ones whose names equalsIgnoreCase("Set-Cookie")) and create a corresponding Cookie header - trimming off everything after the semicolon in each Set-Cookie header, if there is one, and then concatenating all the cookies together with ; as a separator.

This doesn't correctly deal with expiration etc., but this should be enough for most purposes.

这篇关于如何使用App Engine网址抓取服务处理Cookie,例如网络浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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