hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Misha Koshelev <misha...@gmail.com>
Subject Re: Saving cookies with HTTPBuilder/HTTPClient / Re: org.apache.http.impl.cookie.BasicClientCookie not serializable???
Date Thu, 10 Jun 2010 00:13:57 GMT
Ok here is what I ended up going with fyi. Thank you!

    #!/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 java.util.logging.ConsoleHandler
    import java.util.logging.Logger
    import java.util.logging.Level

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

    class HTTPBuilder extends groovyx.net.http.HTTPBuilder {
      def dir=new File("/tmp/dir")
      def idx=1
      def text=null

      public HTTPBuilder() {
        if (dir.exists()) {
          dir.listFiles().each() { file->
    	file.delete()
          }
        }
        dir.mkdir()
      }

      /**
       * Save text and return corresponding XmlSlurper object
       */
      public saveText() {
        def file=new File(dir.toString()+File.separator+idx+".html")
        file<<text
        idx++
        new XmlSlurper(new
org.cyberneko.html.parsers.SAXParser()).parseText(text)
      }

      public Object get(Map<String,?> args) {
        args.contentType=TEXT
        get(args) { resp,reader->
          text=reader.text
          saveText()
        }
      }

      public Object post(Map<String,?> args) {
        args.contentType=TEXT
        post(args) { resp,reader->
          text=reader.text
          saveText()
        }
      }

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

      /**
       * Process Javascript using HTMLUnit
       */
      def processJavascript() {
        def webClient=new WebClient()
        def tempFile=File.createTempFile("HTMLUnit","")
        tempFile<<text
        def page=webClient.getPage("file://"+tempFile.toString())
        text=page.asXml()
        webClient.closeAllWindows()
        tempFile.delete()
        saveText()
      }
    }


On Wed, Jun 9, 2010 at 5:18 PM, Misha Koshelev <misha680@gmail.com> wrote:
> Thank you so much. Is there any good way to save cookies for the 4.0.1
> branch?
>
> I tried per instructions here
> http://www.innovation.ch/java/HTTPClient/advanced_info.html
> System.setProperty("HTTPClient.cookies.save","true")
> System.setProperty("HTTPClient.cookies.jar","/home/misha/.httpclient_cookies")
> but it did not seem to create a file:
> ~/.httpclient_cookies
>
> Thank you!
> Misha
>
> p.s. I am using HTTPBuilder as a backend. Thank you!
>
> http://groovy.codehaus.org/modules/http-builder/
>
> On Wed, 2010-06-09 at 23:34 +0200, Oleg Kalnichevski wrote:
>> On Wed, 2010-06-09 at 14:20 -0500, Misha Koshelev wrote:
>> > Dear All:
>> >
>> > I am quite confused... I am reading here and BasicClientCookie _clearly_
>> > implements Serializable per JavaDoc:
>> > http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/impl/cookie/BasicClientCookie.html
>>
>> You are looking at the javadocs for the 4.1 branch of HttpClient
>>
>> Oleg
>>
>> >
>> > However, my simple Groovy script:
>> >
>> >     #!/usr/bin/env groovy
>> >     @Grapes(
>> >         @Grab(group='org.apache.httpcomponents', module='httpclient',
>> > version='4.0.1')
>> >     )
>> >     import org.apache.http.impl.cookie.BasicClientCookie
>> >     import java.io.File
>> >
>> >     def cookie=new BasicClientCookie("name","value")
>> >     println cookie instanceof Serializable
>> >     def f=new File("/tmp/test")
>> >     f.withObjectOutputStream() { oos->
>> >         oos.writeObject(cookie)
>> >     }
>> >
>> > outputs:
>> >
>> >     false
>> >     Caught: java.io.NotSerializableException:
>> > org.apache.http.impl.cookie.BasicClientCookie
>> >                 at t$_run_closure1.doCall(t.groovy:12)
>> >         at t.run(t.groovy:11)
>> >
>> > I have checked and I have no other versions of HttpClient anywhere in
>> > classpath (if I take Grapes statement out it cannot find file).
>> >
>> > Thank you!
>> > Misha Koshelev
>> >
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> > For additional commands, e-mail: httpclient-users-help@hc.apache.org
>> >
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message