Nick
Nick

Reputation: 5198

Getting URL contents

I've got a server application that responds to HTTP GET requests with an XML response. I'm having trouble making a small Java app to get the contents of this response. Every kind of read I try on the response, or getContent(), or whathaveyou returns nothing.

I first tried the examples listed here: how to get url html contents to string in java

But those use the deprecated readLine() method of InputStream. In the cases above, read()/available() seem to always indicate there's nothing to read (they return -1 and 0, respectively), but give a 200 response code.

Any other URL (http://www.google.com/humans.txt, for example) works fine.

So, I found a more relevant link here: How to read XML response from a URL in java?

Which gave me:

String urlString = new String("http://homeserver/ampache/server/xml.server.php?action=handshake&auth=" + hash + "&timestamp=" + timestamp + "&version=3600013&user=" + user");
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(new URL(urlString).openStream());
System.out.println(doc.toString());

But it looks like I have the same problem:

[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.

Then I tried Apache's HTTPClient, which also returned zilch in the body but with a 200 response code.

The URL is my Ampache installation. It's their XML API: http://homeserver/ampache/server/xml.server.php?action=handshake&auth.... This URL works fine from the browser, and returns what I'm expecting at this point:

<?xml version="1.0" encoding="UTF-8" ?>
<root>
    <error code="403"><![CDATA[Unauthorized access attempt to API - ACL Error]]>
</error>
</root>

Upvotes: 0

Views: 406

Answers (1)

Nick
Nick

Reputation: 5198

It looks like the Ampache URL was malformed, or there is a bug in Ampache's API. The request works fine for http://homeserver/ampache/server/xml.server.php?action=handshake, but not the full on URL with authentication details.

Upvotes: 1

Related Questions