【问题标题】:HtmlUnit Encoding ExceptionHtmlUnit 编码异常
【发布时间】:2013-09-12 15:03:33
【问题描述】:

我尝试使用 HtmlUnit(2.12 版)登录我的大学网站,但我遇到了与网站编码相关的问题

代码:

    try {
        WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_9);
        WebClientOptions options = webClient.getOptions();
        options.setCssEnabled(false);
        options.setThrowExceptionOnScriptError(false);
        options.setThrowExceptionOnFailingStatusCode(false);
        options.setRedirectEnabled(true);
        options.setJavaScriptEnabled(true);   
        HtmlPage page = (HtmlPage) webClient.getPage("http://www.oid.hacettepe.edu.tr/cgi-bin/menuindex.cgi");

    } catch (IOException ex) {
        Logger.getLogger(AnaSayfa.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FailingHttpStatusCodeException ex) {
        Logger.getLogger(AnaSayfa.class.getName()).log(Level.SEVERE, null, ex);
    }

例外:

java.io.UnsupportedEncodingException: İSO-8859-9

我也尝试使用此代码更改字符集:

WebRequest webRequest = new WebRequest(new URL("http://www.oid.hacettepe.edu.tr/cgi-bin/menuindex.cgi"));
webRequest.setCharset("utf-8");
HtmlPage page = (HtmlPage) webClient.getPage(webRequest);

但我的问题还在继续:(请给出解决方案的任何建议

【问题讨论】:

    标签: java htmlunit


    【解决方案1】:

    我以前从未遇到过此类问题。但是,您的代码对我(两者)都很好。这是我得到的输出:

    <?xml version="1.0" encoding="ISO-8859-9"?>
    <html>
      <head>
        <title>
          HU OIDB
        </title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9"/>
        <link rel="shortcut icon" href="http://www.oid.hacettepe.edu.tr/oidb.ico"/>
      </head>
    ...
    </html>
    

    顺便说一句,你注意到你的输出中有一个奇怪的İ 字符吗?

    java.io.UnsupportedEncodingException: İSO-8859-9
    

    基于我可以运行它而你不能运行的事实,我认为它可能是一些操作系统默认值。当然,应该和编码有关。

    您应该尝试更改 JVM 默认字符集。您可以在运行应用程序时使用JAVA_TOOL_OPTIONS 或通过命令行来做到这一点:

    java -Dfile.encoding=UTF-8 -jar YourApp.jar
    

    【讨论】:

    • 问题解决了我用 Locale.setDefault(Locale.ENGLISH);非常感谢你:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2013-12-16
    相关资源
    最近更新 更多