Results 1 to 3 of 3
  1. #1
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    65
    Rep Power
    0

    Default Invalid HTTP response on calling HTTP connection's getInputStream method

    I have a piece of code that connects to musicbrainz' website and queries its database using a simple HttpURLConnection:

    Java Code:
    import java.io.*;import javax.xml.parsers.*;
    import org.w3c.dom.Document;
    import org.xml.sax.SAXException;
    import java.net.*;
    
    
    public class HelloWorld {
        private static final String DB_URL = "http://musicbrainz.org/ws/2/recording?query=";
        
        public static void main (String[] args) {
            System.out.println (new HelloWorld().getDocumentElementFromDatabase());
        }
        
        public Document getDocumentElementFromDatabase() {
        	String query = buildQuery();
        	HttpURLConnection connection = null;
        	
        	try {
        		connection = runQuery (query);
        		return parse (connection.getInputStream());
        	}
        	catch (IOException e) {
        		System.err.println ("Failed to connect to " + DB_URL + query);
        		System.err.println (e.getMessage());
        	}
        	catch (SAXException | ParserConfigurationException e) {
        		e.printStackTrace();
        	}
        	finally {
        		if (connection != null) connection.disconnect();
        	}
        	return null;
        }
        
        private String buildQuery() {
        	StringBuilder builder = new StringBuilder ("\"");
        	builder.append ("Levels");
        	return builder.append ('"').toString();
        }
        
        private HttpURLConnection runQuery (String query) throws MalformedURLException, IOException {
        	return (HttpURLConnection) new URL (DB_URL + query).openConnection();
        }
        
        static Document parse (InputStream inputStream) throws SAXException, IOException, ParserConfigurationException {
        	if (inputStream != null)
        		return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse (inputStream);
        	return null;
        }
    }
    The query seems to run fine, but when it tries to get the InputStream from the connection object, it fails and gives me an error saying Invalid HTTP response.
    Copied from an online compiler:
    Java Code:
    [COLOR=#F0F0F0][FONT=DejaVu Sans Mono]Server returned HTTP response code: 503 for URL: http://musicbrainz.org/ws/2/recording?query="Levels"[/FONT][/COLOR]
    Connecting manually to Levels through the browser works fine.

  2. #2
    Mate de Vita is offline Member
    Join Date
    Mar 2012
    Posts
    65
    Rep Power
    0

    Default Re: Invalid HTTP response on calling HTTP connection's getInputStream method

    I've changed the line
    Java Code:
    query = builder.append ('"').toString()
    to
    Java Code:
    query = URLEncoder.encode (builder.append ('"').toString(), "UTF-8");
    and now at least the error message is consistently the same (I had spaces in some of my links previously):

    Java Code:
    Server returned HTTP response code: 503 for URL: http://musicbrainz.org/ws/2/recording?query=%22Wanted+Dead+or+Alive%22
    Also, calling connection.getErrorStream() after opening the connection returns null even though connection.getResponseCode() returns 503.

    I know 503 means an internal server error, but as stated, the query runs fine if I run it through the browser.

  3. #3
    mmoop is offline Member
    Join Date
    Jun 2015
    Posts
    2
    Rep Power
    0

    Default Re: Invalid HTTP response on calling HTTP connection's getInputStream method

    Quote Originally Posted by Mate de Vita View Post
    Also, calling connection.getErrorStream() after opening the connection returns null even though connection.getResponseCode() returns 503.
    That's because the nothing was sent by the server. They're not required to and usually only do if they want to have a custom error page

    Your code seems to run fine on my machine. Was there a specific set of levels that you were t that are causing the issue?
    I've, in the past, gotten fake 503s when running too many queries from the same ip address during testing. Which is just a security thing,
    Last edited by mmoop; 06-11-2015 at 05:49 AM. Reason: clean up

Similar Threads

  1. Replies: 1
    Last Post: 09-15-2013, 11:42 AM
  2. Replies: 3
    Last Post: 10-15-2012, 11:53 AM
  3. Replies: 1
    Last Post: 03-27-2012, 03:52 AM
  4. Replies: 7
    Last Post: 11-11-2009, 08:29 PM
  5. Getting information about HTTP response method
    By Shiv in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 07:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •