Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to set a timeout on a DocumentBuilder?

Tags:

java

I am currently reading an XML file from a PHP script (as below) which works fine, however I'd now like to add some form of HTTP timeout to retrieving the XML.

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();  
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse("http://www.mywebsite.com/returnsXML");

Can this easily be added given my current approach, or would I need to change the request somehow to support timeouts?

like image 592
Luke Avatar asked Jul 07 '11 10:07

Luke


1 Answers

You can open connection manually and set timeout for URLConnection:

URL url = new URL("http://www.mywebsite.com/returnsXML");
URLConnection con = url.openConnection();
con.setConnectTimeout(10000); // 10 seconds
Document doc = docBuilder.parse(con.getInputStream());
like image 114
Sergey Aslanov Avatar answered Oct 05 '22 23:10

Sergey Aslanov