Is it possible to read the http request and response data from pages loading inside webview. What i want to do is get the binary data from a response after user clicks on a link inside the page in webview. Any help or clue would be greatly appreciated
Asked
Active
Viewed 5,523 times
1 Answers
6
Create your own URLStreamHandlerFactory initialized by URL.setURLStreamHandlerFactory which generates a URLStreamHandler that wraps the standard http and https URLStreamHandlers to intercept their traffic before forwarding.
Some of the concepts are explained in A New Era for Java Protocol Handlers whitepaper.
Another option is to listen to the WebEngine.location property and open a separate connection to a server to retrieve and process the binary data as needed. An example of this approach is the pdf handling code for the willow web browser.

jewelsea
- 150,031
- 14
- 366
- 406
-
Hello Jewelsea, I know my comment is not relevant to the question and answer, and its not a comment after all, but would you be willing to give some more insight on the problem I am facing regarding JavaFx Webview and an Oracle ADF based website that I need to access. If this comment is inappropriate or if I have to go somewhere else to request something like that moderator please advise. Thanks – bluchip.gr Jul 04 '13 at 11:46
-
Sorry bluechip, I can't help you with your problem with WebView and ADF as I have no idea what that problem is and have never used ADF before. You can create a new question which describes your ADF issues in more detail (providing an [SSCCE](http://sscce.org/) often helps) and somebody familiar with that technology may be able to assist you. You can also post to [Oracle forums](https://forums.oracle.com/community/developer/english/java/javafx/javafx_2.0_and_later). – jewelsea Jul 04 '13 at 14:53
-
Will do, thanks for the tips and advise. I'll try to explain my problem in more depth both here and Oracle forums. Again thanks for your help – bluchip.gr Jul 04 '13 at 15:38
-
I am trying to implement the solution you suggest above with my own URLStreamHandlerFactory but I am having difficulties understanding how to achieve what I want. I posted another question [here](http://stackoverflow.com/questions/17522343/custom-javafx-webview-protocol-handler) with my progress. You seem to know alot about webview and java communication protocol so I kindly ask for some of your time to have a look and comment on what I am doing wrong. Thank you – bluchip.gr Jul 08 '13 at 09:57
-
could you please explain more? or add a source code to show to process? Thank you! – Sina Barghidarian Oct 16 '14 at 16:11
-
3So my choices are to hack a core mechanism, essential to the classloading process, via a JVM-global write-once property... Or double my bandwidth by fetching everything twice... The more I learn about the JavaFx WebView, the more I suspect it was written by Franz Kafka – Kevin Wright Aug 02 '18 at 14:21
-
2This *was* a great answer, and one I had done myself with some success. However for javafx 12 and up the web engine uses an internal http implementation and not the URLStreamHandlerFactory. You can work around this by setting the "com.sun.webkit.useHTTP2Loader" system property to false, but it's not clear if that will stay a viable solution long-term. – Sean Reilly Sep 02 '20 at 12:37
-
The class loading process does call into the URLStreamHandlerFactory, but if your custom factory returns null then it uses the built-in default factory, so I don't think that's a worry. I agree with @KevinWright that this could all be so much simpler if the WebView simply had a hook for handling responses. Being able to check responses for a content-disposition: attachment header seems like an obvious requirement. – Sean Reilly Sep 02 '20 at 12:56