【问题标题】:Valid URI results in Content-Length: -1有效 URI 结果为 Content-Length: -1
【发布时间】:2013-06-05 19:08:59
【问题描述】:

从 Java 7u21 开始,我在自己制作的 JavaFX 小程序中偶然发现了一些奇怪的行为。

在服务器端,我有一个 Glassfish 3 服务器和一个 mysql 数据库。在客户端,我使用 Jersey 通过向我的 Glassfish 服务器发送请求来从数据库中获取数据。当我通过 JAR 文件启动程序时一切正常,但是当我通过 JNLP 文件启动它时,我注意到自 7u21 以来的一些奇怪行为。让我试着给你看:

我做了一个简单的请求,它应该给出一个 XML 响应:

方法(客户端):

public List<Users> getUsersHierarchyTreeFilteredByUserlevel(int id, int betweenStart, int betweenEnd) throws UniformInterfaceException {
    WebResource resource = webResource;
    resource = resource.path(java.text.MessageFormat.format("users/{0}/hierarchy", String.valueOf(id))).queryParam("start", "" + String.valueOf(betweenStart)).queryParam("end", "" + String.valueOf(betweenEnd));
    System.out.println(resource.getURI());
    return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(new GenericType<List<Users>>() {});
}

URISystem.out.println(resource.getURI());

http://localhost:8080/myWS/webresources/entities.users/users/4/hierarchy?start=5&end=5

Java 控制台:

network: Cache entry not found [url: http://localhost:8080/myWS/webresources/entities.users/users/4/hierarchy?start=5&end=5, version: null]
network: Connecting http://localhost:8080/myWS/webresources/entities.users/users/4/hierarchy?start=5&end=5 with proxy=DIRECT
network: Connecting socket://localhost:8080 with proxy=DIRECT
network: Downloading resource: http://localhost:8080/myWS/webresources/entities.users/users/4/hierarchy?start=5&end=5
Content-Length: -1
Content-Encoding: null
network: Wrote URL http://localhost:8080/myWS/webresources/entities.users/users/4/hierarchy?start=5&end=5 to File C:\Users\Steven\AppData\LocalLow\Sun\Java\Deployment\cache\6.0\33\d7edc21-130fad10-temp
cache: Adding MemoryCache entry: http://localhost:8080/myWS/webresources/entities.users/users/4/hierarchy
java.io.IOException: stream is closed

如您所见,我收到了 IOException。这似乎发生在 getUsersHierarchyTreeFilteredByUserlevel(...) 方法中的 resource.accept(...) 处。但是,当我将 URI 粘贴到浏览器中时,它会显示正确的 XML 数据吗?我不知道为什么......它在运行 JAR 本身或使用 7u17 时有效。

有什么想法吗? 提前致谢!

更新 *使用 wget(64 位)获取*

D:\>WGET64.EXE -S --http-user=*** --http-passwd=*** "http://localhost:8080/myWS/webresources/entities.users/users/4/hierarchy?start=5&end=5"
--22:06:53--  http://localhost:8080/myWS/webresources/entities.users/users/4/hierarchy?start=5&end=5
       => `hierarchy@start=5&end=5'
Resolving localhost... 127.0.0.1
Connecting to localhost[127.0.0.1]:8080... connected.
HTTP request sent, awaiting response...
 1 HTTP/1.1 200 OK
 2 X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)
 3 Server: GlassFish Server Open Source Edition 3.1.2.2
 4 Pragma: No-cache
 5 Cache-Control: no-cache
 6 Expires: Thu, 01 Jan 1970 01:00:00 CET
 7 Content-Type: application/xml
 8 Date: Wed, 05 Jun 2013 20:06:53 GMT
 9 Connection: close

    [ <=>                                 ] 9,610         --.--K/s

22:06:53 (9.16 MB/s) - `hierarchy@start=5&end=5' saved [9610]

【问题讨论】:

  • 如果您尝试使用命令行客户端获取 URI(例如 wget-S 选项),内容长度是否正确?
  • 顺便说一句,您似乎想从各种参数构建 URI;在这种情况下,this 可以提供帮助。
  • 嗨 fge,刚刚进行了 wget 抓取。在原始帖子中添加了调查结果。
  • 嗯,你确定你的 Java 代码也没有 401 吗?你可以尝试使用另一个 API 来做一个简单的 main() 吗?
  • 当然最好使用 wget 的所有正确参数。我使用 BASIC 身份验证,所以必须添加 --http-user 和 --http-passwd。无论如何,用正确的 wget 数据更新问题。

标签: java rest jersey uri


【解决方案1】:

这都与关于这个问题的相同问题有关:

Authentication required window popping up after 7u21 update

并跟进问题:

basic authentication fails with glassfish

由 dennis-the-menace 回答,您可以在此处找到:

https://stackoverflow.com/a/18362271/1527284

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    相关资源
    最近更新 更多