Question

I am launching requests to a web service using Groovy/XmlSlurper:

new XmlSlurper().parse("http://en.wikipedia.org/w/api.php?action=...")

I would like to set the user agent to let the website know what project the requests are from.

How to set the user agent?

  • XMLSlurper's constructors do not have anything like this.
  • The setProperty method sounds interesting but Google does not lead to anything related to UA.
  • I would rather avoid changing the code to abandon XmlSlurper in favor of another library.
Was it helpful?

Solution

Looks like Groovy uses the JVM's default SAX parser to connect to the input stream and perform the initial parsing. I saw this answer on SO that might help - all you need to do is set this before your call to the slurper to make it happen:

System.setProperty("http.agent", "my-agent-name");

P.S. This is just a guess - hope it helps.

OTHER TIPS

I see p.3, btw there is also Groovy Http Builder, it uses JsonSlurper for JSON results, but it's much more flexible for making http requests - http://groovy.codehaus.org/HTTP+Builder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top