Results 1 to 3 of 3
- 04-10-2015, 07:38 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 65
- Rep Power
- 0
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; } }
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]
- 04-11-2015, 12:28 PM #2
Member
- Join Date
- Mar 2012
- Posts
- 65
- Rep Power
- 0
Re: Invalid HTTP response on calling HTTP connection's getInputStream method
I've changed the line
Java Code:query = builder.append ('"').toString()
Java Code:query = URLEncoder.encode (builder.append ('"').toString(), "UTF-8");
Java Code:Server returned HTTP response code: 503 for URL: http://musicbrainz.org/ws/2/recording?query=%22Wanted+Dead+or+Alive%22
I know 503 means an internal server error, but as stated, the query runs fine if I run it through the browser.
- 06-11-2015, 05:47 AM #3
Member
- Join Date
- Jun 2015
- Posts
- 2
- Rep Power
- 0
Re: Invalid HTTP response on calling HTTP connection's getInputStream method
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
-
The specified HTTP method is not allowed for the requested resource (HTTP method POST
By bhanu122 in forum Advanced JavaReplies: 1Last Post: 09-15-2013, 11:42 AM -
HTTP status 405 HTTP method GET is not supported by this URL
By mDennis10 in forum Web FrameworksReplies: 3Last Post: 10-15-2012, 11:53 AM -
The specified HTTP method is not allowed for the requested resource (HTTP method GET
By Jarran in forum Java ServletReplies: 1Last Post: 03-27-2012, 03:52 AM -
HTTP Status 405 - HTTP method GET is not supported by this URL
By javanewbie in forum Java ServletReplies: 7Last Post: 11-11-2009, 08:29 PM -
Getting information about HTTP response method
By Shiv in forum New To JavaReplies: 0Last Post: 04-10-2009, 07:58 PM
Bookmarks