如何在 HTTP Builder 0.5.0/HTTPClient 中保存 cookie

发布于 2024-09-05 13:25:01 字数 495 浏览 8 评论 0原文

我正在尝试按照此处的说明进行操作: http://www.innovation.ch/java/HTTPClient/advanced_info.html

但是,如果我使用 HTTP Builder,以下几行

System.setProperty("HTTPClient.cookies.save","true")
System.setProperty("HTTPClient.cookies.jar","/home/misha/.httpclient_cookies")

似乎不会创建文件:

~/.httpclient_cookies

当弄清楚时,我将一如既往地发布解决方案。

:)

米莎

I am trying per instructions here:
http://www.innovation.ch/java/HTTPClient/advanced_info.html

However, if I am using HTTP Builder, the following lines

System.setProperty("HTTPClient.cookies.save","true")
System.setProperty("HTTPClient.cookies.jar","/home/misha/.httpclient_cookies")

do not seem to create a file:

~/.httpclient_cookies

I will post a solution as always when figure it out.

:)

Misha

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就此别过 2024-09-12 13:25:01

您链接的 HTTPClient 与与 groovy HTTPBuilder 捆绑在一起的 apache HTTPClient 不同。查看此文档以使用Apache HTTPClient。

The HTTPClient you've linked is not the same as the apache HTTPClient that's bundled with the groovy HTTPBuilder. Take a look at this documentation for persisting cookies with apache HTTPClient.

记忆で 2024-09-12 13:25:01

谢谢。我采用了一个更黑客的解决方案

#!/usr/bin/env groovy

import com.gargoylesoftware.htmlunit.WebClient

import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

import java.io.File

import org.apache.http.impl.cookie.BasicClientCookie

class HTTPBuilder extends groovyx.net.http.HTTPBuilder {

......

  /**
   * Load cookies from specified file
   */
  def loadCookies(file) {
    file.withObjectInputStream { ois->
      ois.readObject().each { cookieMap->
    def cookie=new BasicClientCookie(cookieMap.name,cookieMap.value)
    cookieMap.remove("name")
    cookieMap.remove("value")
    cookieMap.entrySet().each { entry->
      cookie."${entry.key}"=entry.value
    }
    client.cookieStore.addCookie(cookie)
    println cookie
      }
    }
  }

  /**
   * Save cookies to specified file
   */
  def saveCookies(file) {
    def cookieMaps=new ArrayList(new LinkedHashMap())
    client.cookieStore.getCookies().each { cookie->
      def cookieMap=[:]
      cookieMap.version=cookie.version
      cookieMap.name=cookie.name
      cookieMap.value=cookie.value
      cookieMap.domain=cookie.domain
      cookieMap.path=cookie.path
      cookieMap.expiryDate=cookie.expiryDate
      cookieMaps.add(cookieMap)
      println cookie
    }
    file.withObjectOutputStream { oos->
      oos.writeObject(cookieMaps)
    }
  }

}

Thank you. I went with a hackier solution:

#!/usr/bin/env groovy

import com.gargoylesoftware.htmlunit.WebClient

import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT

import java.io.File

import org.apache.http.impl.cookie.BasicClientCookie

class HTTPBuilder extends groovyx.net.http.HTTPBuilder {

...

  /**
   * Load cookies from specified file
   */
  def loadCookies(file) {
    file.withObjectInputStream { ois->
      ois.readObject().each { cookieMap->
    def cookie=new BasicClientCookie(cookieMap.name,cookieMap.value)
    cookieMap.remove("name")
    cookieMap.remove("value")
    cookieMap.entrySet().each { entry->
      cookie."${entry.key}"=entry.value
    }
    client.cookieStore.addCookie(cookie)
    println cookie
      }
    }
  }

  /**
   * Save cookies to specified file
   */
  def saveCookies(file) {
    def cookieMaps=new ArrayList(new LinkedHashMap())
    client.cookieStore.getCookies().each { cookie->
      def cookieMap=[:]
      cookieMap.version=cookie.version
      cookieMap.name=cookie.name
      cookieMap.value=cookie.value
      cookieMap.domain=cookie.domain
      cookieMap.path=cookie.path
      cookieMap.expiryDate=cookie.expiryDate
      cookieMaps.add(cookieMap)
      println cookie
    }
    file.withObjectOutputStream { oos->
      oos.writeObject(cookieMaps)
    }
  }

...

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文