Getting a reference to Java's default http(s) URLStreamHandler

后端 未结 3 1367
粉色の甜心
粉色の甜心 2020-12-17 01:37

I have a library that I need to use in one of my projects, which unfortunately registers its own URLStreamHandler to handle http-URLs. Is there a w

3条回答
  •  星月不相逢
    2020-12-17 02:31

    You can register your own URLStreamHandlerFactory wirh URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory());

    public class URLStreamHandlerFactory implements java.net.URLStreamHandlerFactory {
        public URLStreamHandlerFactory() {}
    
        public URLStreamHandler createURLStreamHandler(String protocol) {
            if(protocol.equals("http")) {
                return new sun.net.www.protocol.http.Handler();
            } else if(protocol.equals("https")) {
                return new sun.net.www.protocol.https.Handler();
            }
            return null;
        }
    }
    

    so you can use standard handler

    EDIT: Found this code

提交回复
热议问题