【发布时间】:2012-07-26 06:05:03
【问题描述】:
这是我使用打开 URLConnection 到另一个 webapllication 的代码
try {
URL url = new URL("https://mySecondWebApp/Cart");
HttpsURLConnection conn1 = (HttpsURLConnection) url.openConnection();//line1
conn1 .connect();//line 2 does not establishes the connection
conn1.getInputStream();// line 3 works which means that i get control to my app2 in debugger.But this also passes the control to IOException stating error java.io.FileNotFoundException: https://mySecondWebApp/Cart
}
catch (MalformedURLException e) {
log.error("MalformedURLException" + e);
}
catch (IOException e) {
log.info("getting IO error");
}
我不明白为什么第 2 行没有与 app2 建立连接,而第 3 行却建立了连接? 第二件事是为什么我在第 3 行之后得到文件 notfound 异常,即使它成功连接到我想避免的 app2。我的意图 只是为了建立与 app2 的连接,以便我在 app2 的 java 代码中获得控制权
【问题讨论】:
-
试试看HttpsURLConnection的代码,或者通过API doc,你会得到答案
-
根据 docs.oracle.com/javase/tutorial/networking/urls/connecting.html,看起来 connect 和 get inputstream 都应该连接到 app2 。请参阅您并不总是需要的行明确地...在链接中。但在我的情况下,只有 getinputstream 这样做?不知道为什么?当我执行 conn1.getInputStream() 时,也没有得到为什么我得到 filenotfound 异常
标签: java jakarta-ee urlconnection httpsurlconnection