views:

202

answers:

2

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

A: 

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.

ataylor
A: 

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)
    }
  }

...

}
Misha Koshelev