Search code examples
javajframehttp-unit

httpunit isn't working when Class extends JFrame


When attempting to login to https://www.okcupid.com/login with the httpunit library, the code below executes successfully.

If, however, I extend the class with JFrame so that I can build the UI, the execution fails. The POST request completes properly but the GET request fails when executing HtmlPage page = wc.getPage(url)

Below the code I have put the output for both scenarios.

Works unless Main extends JFrame

public class Main /* extends JFrame */ {
    static boolean isConnecting = false;
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        login();
    }

    public void login() {
        try {
            /* POST */
            WebClient wc = new WebClient();
            URL url = new URL("https://www.okcupid.com/login");
            WebRequest requestSettings = new WebRequest(url, HttpMethod.POST);
            requestSettings.setRequestBody("username=myLogin&password=myPassword&okc_api=1");
            Page redirectPage = wc.getPage(requestSettings);/* unique */
            Set<Cookie> cookies = wc.getCookieManager().getCookies();

            /*GET */
            wc = new WebClient();
            for (Cookie cookie : cookies) {
                wc.getCookieManager().addCookie(cookie);
            }
            HtmlPage page = wc.getPage(url);
            WebResponse response = page.getWebResponse();
            String content = response.getContentAsString();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output when Main extends JFrame

Connected to the target VM, address: '127.0.0.1:52571', transport: 'socket'
Apr 21, 2016 5:31:20 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
Apr 21, 2016 5:31:20 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: :x).] sourceName=[https://includes.okccdn.com/flat/js/vendor.min.js?v=7a3cb7237dd753a] line=[24] lineSource=[null] lineOffset=[0]
Exception in thread "main" java.lang.StackOverflowError
    at net.sourceforge.htmlunit.corejs.javascript.regexp.NativeRegExp.emitREBytecode(NativeRegExp.java:1311)
    at net.sourceforge.htmlunit.corejs.javascript.regexp.NativeRegExp.emitREBytecode(NativeRegExp.java:1281)
.
.
.
.
.
    // (this repeats well over a few thousand times before finishing with exit code 1.
    at net.sourceforge.htmlunit.corejs.javascript.regexp.NativeRegExp.emitREBytecode(NativeRegExp.java:131
.
.
.
.
.
Disconnected from the target VM, address: '127.0.0.1:52571', transport: 'socket'

Process finished with exit code 1

Output when Main does not extend JFrame

Connected to the target VM, address: '127.0.0.1:52635', transport: 'socket'
Apr 21, 2016 5:38:29 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
Apr 21, 2016 5:38:30 PM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: '*,:x' error: Invalid selector: :x).] sourceName=[https://includes.okccdn.com/flat/js/vendor.min.js?v=7a3cb7237dd753a] line=[24] lineSource=[null] lineOffset=[0]
Apr 21, 2016 5:38:30 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'https://includes.okccdn.com/lang/en/version/219b51f028bbb87/globals.css' [3:9674] Error in style sheet. (Invalid token "@charset". Was expecting one of: <EOF>, <S>, <IDENT>, "<!--", "-->", ".", ":", "*", "[", <HASH>, <IMPORT_SYM>, <PAGE_SYM>, <MEDIA_SYM>, <FONT_FACE_SYM>, <ATKEYWORD>.)
Apr 21, 2016 5:38:30 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'https://includes.okccdn.com/lang/en/version/df13e6172aa8212/locals.css?set=home2015,flag_photo' [3:1512] Error in expression. (Invalid token " ". Was expecting one of: <NUMBER>, "inherit", <IDENT>, <STRING>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <RESOLUTION_DPI>, <RESOLUTION_DPCM>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>.)
Apr 21, 2016 5:38:30 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'https://includes.okccdn.com/lang/en/version/df13e6172aa8212/locals.css?set=home2015,flag_photo' [3:1543] Error in expression. (Invalid token " ". Was expecting one of: <NUMBER>, "inherit", <IDENT>, <STRING>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <RESOLUTION_DPI>, <RESOLUTION_DPCM>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>.)
Apr 21, 2016 5:38:30 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'https://includes.okccdn.com/lang/en/version/df13e6172aa8212/locals.css?set=home2015,flag_photo' [3:1566] Error in expression. (Invalid token " ". Was expecting one of: <NUMBER>, "inherit", <IDENT>, <STRING>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <RESOLUTION_DPI>, <RESOLUTION_DPCM>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>.)
Apr 21, 2016 5:38:30 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'https://includes.okccdn.com/lang/en/version/df13e6172aa8212/locals.css?set=home2015,flag_photo' [3:3949] Error in expression. (Invalid token " ". Was expecting one of: <NUMBER>, "inherit", <IDENT>, <STRING>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <RESOLUTION_DPI>, <RESOLUTION_DPCM>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>.)
Apr 21, 2016 5:38:30 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'https://includes.okccdn.com/lang/en/version/df13e6172aa8212/locals.css?set=home2015,flag_photo' [3:3982] Error in expression. (Invalid token " ". Was expecting one of: <NUMBER>, "inherit", <IDENT>, <STRING>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <RESOLUTION_DPI>, <RESOLUTION_DPCM>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>.)
Apr 21, 2016 5:38:30 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'https://includes.okccdn.com/lang/en/version/df13e6172aa8212/locals.css?set=home2015,flag_photo' [3:4007] Error in expression. (Invalid token " ". Was expecting one of: <NUMBER>, "inherit", <IDENT>, <STRING>, <HASH>, <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <RESOLUTION_DPI>, <RESOLUTION_DPCM>, <PERCENTAGE>, <DIMENSION>, <URI>, <FUNCTION>.)

.
.
.
.
.
.
.
.
.
 and so on, and so forth
.
.
.
.
.
.
.
<!DOCTYPE html>
    DESIRED HTML RESPONSE STRING
</html>
.
.

// After connecting successfully, the following continues to output every 30 seconds
Apr 21, 2016 5:39:00 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'https://1-instant.okcupid.com/instantevents?random=0.437989956299249', but got 'text/html'.
Apr 21, 2016 5:39:29 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'https://2-instant.okcupid.com/instantevents?random=0.4299430644437371&server_seqid=0&server_gmt=1461274740', but got 'text/html'.

Solution

  • I figured out why this is happening and a way around it but I still don't know how to make it work extending JFrame.

    1. If you're using htmlunit to handle URL requests, first remove the JFrame extension. Now the code will execute but still not launch the UI.

    2. Instead of using the Class's extension for JFrame, in the part where you'll need the extension, create a new JFrame instance. Then use that instance of JFrame to declare all necessary properties and actions.