5

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?

2
  • 2
    Nice catch, though URLConnection is documented to use HTTP1.1 which requires the response to provide a status line in the headers. Only under HTTP1.0 this would be a valid HTTP response. Commented Nov 13, 2013 at 13:35
  • 1
    mabi: not even under 1.0. In 0.9, yes. Commented Nov 13, 2013 at 13:54

1 Answer 1

4

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,

Sign up to request clarification or add additional context in comments.

1 Comment

A beautifully simple solution. Thank you. On my platform (Linux) I had to modify the line termination. So, I used out.print("GET " + relativeURL + " HTTP/1.1 \r\n\r\n");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.