2

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.
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373

2 Answers2

2

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.

Community
  • 1
  • 1
RonK
  • 9,472
  • 8
  • 51
  • 87
0

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

Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113