I'm developing an Java application( J2SE ). In my application, I should send a message via a queue( using JDNI of Weblogic ) and then should interact with HTTP web url ( using an URLConnection ).
If I try to interact with HTTP web using URLConnection, it is succeed. But, after a message is sending by a Queue, interaction with HTTP web(using URLConnection) is always failed. After I extremely reduce source code, finally I found that code for setting JDNI impacted on making UrlConnection object.
Below is source code for explanation.
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
Even though InitialContext() method call finished, I expect to return HttpURLConnection, but actually SOAPHttpURLConnection has returned by url.openConnction(); So, I can not interact with web page using that.
Why does the SOAPHttpURLConnection has returned ?? And How can I resolve this problem??
You can force weblogic to use the Default Handler by setting system property UseSunHttpHandler. Add
-DUseSunHttpHandler=true
to your JVM Arguments. I understand you are not running inside weblogic container but just a J2SE code,but I guess weblogic context factory class or some weblogic library code is registering URLStreamHandlerFactory which is returning weblogic http handlers. The reason why i say this is that the URL class of JDK caches the handlers. So in your case during first request where it returned HTTP handler from sun default pkg it should have cached it and always return the same handler from the cache. Only possible case when JDK clears this Handler cache is if somebody registers a new URLStreamHandlerFactory.Other Option to verify this: After Initialcontext is initialized try invoking the URL.setURLStreamHandlerFactory. If weblogic has already registered a factory you should get an exception as JVM allows that method to be invoked only once.