简体   繁体   English

当Class扩展JFrame时,httpunit不起作用

[英]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. 尝试使用httpunit库登录https://www.okcupid.com/login时,以下代码成功执行。

If, however, I extend the class with JFrame so that I can build the UI, the execution fails. 但是,如果我使用JFrame扩展类以便构建UI,则执行失败。 The POST request completes properly but the GET request fails when executing HtmlPage page = wc.getPage(url) POST请求正确完成,但执行HtmlPage page = wc.getPage(url)GET请求失败

Below the code I have put the output for both scenarios. 在代码下面我已经为两个场景输出了输出。

Works unless Main extends JFrame 除非Main扩展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 Main扩展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 Main不扩展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'.

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. 我弄清楚为什么会发生这种情况并解决它,但我仍然不知道如何使其扩展JFrame。

  1. If you're using htmlunit to handle URL requests, first remove the JFrame extension. 如果您使用htmlunit处理URL请求,请先删除JFrame扩展。 Now the code will execute but still not launch the UI. 现在代码将执行但仍然不启动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. 在您需要扩展的部分中,不是使用Class的JFrame扩展,而是创建一个新的JFrame实例。 Then use that instance of JFrame to declare all necessary properties and actions. 然后使用该JFrame实例声明所有必需的属性和操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM