我正在开发一个 Java 应用程序( J2SE )。在我的应用程序中,我应该通过队列发送消息(使用 Weblogic 的 JDNI),然后应该与 HTTP Web url 交互(使用 URLConnection)。
如果我尝试使用 URLConnection 与 HTTP Web 交互,它会成功。但是,在队列发送消息后,与 HTTP 网络(使用 URLConnection)的交互总是失败。在我极大地减少了源代码之后,我终于发现设置JDNI的代码影响了UrlConnection对象的制作。
下面是解释的源代码。
private void test() throws IOException, NamingExcpetion {
HttpUrlConnection c1 = getConnection();
System.out.println(c1);
initContext();
HttpUrlConnection c2 = getConnection();
System.out.println(c2);
}
public void initContext() throws ... {
Properties prop = new Properties();
Prop.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
Prop.put(Context.PROVIDER_URL, "t3://111.111.111.111:7100"); // my target weblogic server having queues.
new InitialContext(prop);
System.out.println("InitialContext() finished!!");
}
public HttpURLConnection getConnection() throws .... {
URL url = new URL("http://222.222.222.222:8100/login"); // My target server to be interacting via URLConnection; I'm using java.net.URL
return (HttpURLConnection) url.openConnection();
}
And below is the result.
sun.net.www.protocol.http.HttpURLConnection:http://222.222.222.222:8100/login
InitialContext() finished!!
weblogic.net.http.SOAPHttpURLConnection:http://222.222.222.222:8100/login
即使 InitialContext() 方法调用完成,我希望返回 HttpURLConnection,但实际上 SOAPHttpURLConnection 已通过 url.openConnction(); 返回 所以,我不能使用它与网页交互。
为什么 SOAPHttpURLConnection 已经返回?我该如何解决这个问题?