我正在开发一个Java应用程序(J2SE)。在我的应用程序中,我需要通过队列(使用Weblogic的JDNI)发送消息,然后应该与HTTP Web URL进行交互(使用URLConnection)。
如果我尝试使用URLConnection与HTTP Web进行交互,则会成功。但是,在通过队列发送消息后,使用URLConnection与HTTP Web进行交互始终失败。经过极大地减少源代码,最终我发现设置JDNI的代码影响了UrlConnection对象的生成。
以下是解释的源代码。
尽管InitialContext()方法调用已经完成,但我期望返回HttpURLConnection,但实际上由url.openConnection()返回了SOAPHttpURLConnection。因此,我无法使用它与网页交互。
为什么会返回SOAPHttpURLConnection?如何解决这个问题?
如果我尝试使用URLConnection与HTTP Web进行交互,则会成功。但是,在通过队列发送消息后,使用URLConnection与HTTP Web进行交互始终失败。经过极大地减少源代码,最终我发现设置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,但实际上由url.openConnection()返回了SOAPHttpURLConnection。因此,我无法使用它与网页交互。
为什么会返回SOAPHttpURLConnection?如何解决这个问题?
URLStreamHandlers
)。 - Kayaman