문제

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.
도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top