httpunit isn't working when Class extends JFrame

时间:2016-04-21 21:59:36

标签: java jframe http-unit

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'.

1 个答案:

答案 0 :(得分:0)

我弄清楚为什么会发生这种情况并解决这个问题,但我仍然不知道如何让它扩展JFrame。

  1. 如果您正在使用htmlunit处理网址请求,请先删除JFrame扩展程序。现在代码将执行但仍然不启动UI。

  2. 不要使用JFrame的Class扩展,而是在您需要扩展的部分中,创建一个新的JFrame实例。然后使用该JFrame实例声明所有必需的属性和操作。