Some devices (e.g. webrelays) return raw XML in response to HTTPGet requests. That is, the reply contains no valid HTTP header. For many years I have retrieved information from such devices using code like this:
private InputStream doRawGET(String url) throws MalformedURLException, IOException
{
try
{
URL url = new URL(url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
return con.getInputStream();
}
catch (SocketTimeoutException ex)
{
throw new IOException("Timeout attempting to contact Web Relay at " + url);
}
}
In openJdk 7 the following lines have been added to sun.net.www.protocol.http.HttpURLConnection, which mean any HTTP response with an invalid header generates an IOException:
1325 respCode = getResponseCode();
1326 if (respCode == -1) {
1327 disconnectInternal();
1328 throw new IOException ("Invalid Http response");
1329 }
How do I get 'headless' XML from a server which expects HTTPGet requests in the new world of Java 7?
You could always do it the "socket way" and talk HTTP directly to the host:
private InputStream doRawGET( String url )
{
Socket s = new Socket( new URL( url ).getHost() , 80 );
PrintWriter out = new PrintWriter( s.getOutputStream() , true );
out.println( "GET " + new URL( url ).getPath() + " HTTP/1.0" );
out.println(); // extra CR necessary sometimes.
return s.getInputStream():
}
Not exactly elegant, but it'll work. Strange that JRE7 introduces such "regression" though.
Cheers,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With