2015-01-16 54 views
2

有点背景,我想创建一个URL Stream Handler,这样我就可以跟踪我在我的web浏览器中有多少连接在我的javafx中应用。实质上,我在WebView中运行一个AngularJs应用程序,我想知道它何时完成。我无法触摸网站代码,因此添加js通知程序不在桌面上。所以,不管我把什么放在一起,总是出现'协议不支持输入'的错误。我试图用只返回false的方法覆盖'getDoInput',但我仍然得到错误。有任何想法吗?为URLStreamHandlerFactory创建HttpURLConnection,获取错误:协议不支持输入

下面是一些接近我在做什么:

public class MyUrlStreamHandlerFactory implements URLStreamHandlerFactory { 

    public URLStreamHandler createURLStreamHandler(String protocol) { 
     if (protocol.equalsIgnoreCase("http") || protocol.equalsIgnoreCase("https")) { 

      return new URLStreamHandler() { 
       @Override 
       protected URLConnection openConnection(URL url) throws IOException { 

        return new HttpURLConnection(url) { 
         @Override 
         public void connect() throws IOException { 

         } 

         @Override 
         public void disconnect() { 

         } 

         @Override 
         public boolean usingProxy() { 
          return false; 
         } 

         @Override 
         public boolean getDoInput() { 
          return false; 
         } 
        }; 

       } 
      }; 

     } 
     return null; 
    } 
} 

我与安装它:

URL.setURLStreamHandlerFactory(new MyUrlStreamHandlerFactory()); 
+0

我们唯一能做的就是声明*“这里接近我正在做的事情”*是猜测可能导致错误的原因。请将相关代码发布到您实际正在做的事情上。 –

+0

这是代码的当前状态,因为它存在于项目中,除了int成员和++以外,以及 - 分别在连接和断开连接内。我忽略了这一点,因为增加和减少对这个问题无害。我还没有进一步发展,因为我无法确定它为什么会产生错误“协议不支持输入”。 –

回答

1

我明白你不过要完成什么,我觉得这是错误的方式去做。

From:Java Network Programming by Elliotte Rusty Harold

Only abstract URLConnection classes are present in the java.net package. The concrete subclasses are hidden inside the sun.net package hierarchy. It is rare to instantiate URLConnection objects directly in your source code; instead, the runtime environment creates these objects as needed, depending on the protocol in use. The class (which is unknown at compile time) is then instantiated using the forName() and newInstance() methods of the java.lang.Class class.

For example, the connect() method of sun.net.www.protocol.http.HttpURLConnection creates a sun.net.www.http.HttpClient object, which is responsible for connecting to the server.

所以,除非你想编写自己的HTTP协议处理程序和HttpClient的,我建议探索其他途径。

其他事项

唯一的方法,我能找到,抛出该消息是"protocol doesn't support input"UnknownServiceException是:

java.net.URLConnection中的#的getInputStream

/** 
* Returns an input stream that reads from this open connection. 
* 
* @return  an input stream that reads from this open connection. 
* @exception IOException    if an I/O error occurs while 
*    creating the input stream. 
* @exception UnknownServiceException if the protocol does not support 
*    input. 
*/ 
public InputStream getInputStream() throws IOException { 
    throw new UnknownServiceException("protocol doesn't support input"); 
} 

重写getDoInput
您不应该覆盖getDoInput只有r eturn错误。相反,你应该使用setDoInput(false)。但是,您不想将doInput设置为false。你总是想读一些东西,比如响应代码。

+1

我最终扩展了sun.net.www.protocol.http.HttpURLConnection,它与第一部分的答案一致。 –

+0

@freeman_irl你是如何重写'getInputStream()'然后呢? – soommy12

相关问题