我有一个服务器应用程序,它响应带有XML响应的HTTP GET请求。我无法制作一个小型Java应用程序来获取此响应的内容。每种阅读我都会尝试回复,或者getContent()
,或者什么都不会返回。获取URL内容
我第一次尝试在这里列出的例子: how to get url html contents to string in java
但InputStream的那些使用过时的readLine()方法。在上面的情况下,read()
/available()
似乎总是表示没有任何可读的内容(它们分别返回-1和0),但给出200响应代码。
任何其他URL(例如http://www.google.com/humans.txt)都可以正常工作。
所以,我在这里找到更相关链接: How to read XML response from a URL in java?
这给了我:
String urlString = new String("http://homeserver/ampache/server/xml.server.php?action=handshake&auth=" + hash + "×tamp=" + timestamp + "&version=3600013&user=" + user");
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(new URL(urlString).openStream());
System.out.println(doc.toString());
但它看起来像我有同样的问题:
[Fatal Error] :1:1: Premature end of file.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
然后我尝试了Apache的HTTPClient,它也在主体中返回了zilch,但有200个响应代码。
该URL是我的Ampache安装。这是他们的XML API:http://homeserver/ampache/server/xml.server.php?action=handshake&auth...
。这个URL从浏览器工作正常,并返回什么,我在这一点期待:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<error code="403"><![CDATA[Unauthorized access attempt to API - ACL Error]]>
</error>
</root>
你能给我们展示一些代码吗?没有代码,很难说你做错了什么。 –
@UwePlonus,已添加。 – Nick