Search code examples
cross-domainhtmlunit

How to solve the cross domain in HtmlUnit


Error:

六月 21, 2016 4:15:06 下午 com.gargoylesoftware.htmlunit.xml.XmlPage <init>
警告: Failed parsing XML document http://live3.win007.com/vbsxml/goalBf3.xml?r=0071466496906000: Content is not allowed in prolog.
六月 21, 2016 4:15:06 下午 com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine handleJavaScriptException
信息: Caught script exception
======= EXCEPTION START ========
EcmaError: lineNumber=[41] column=[0] lineSource=[<no source>] name=[TypeError] sourceName=[http://live3.win007.com/common2.js] message=[TypeError: Cannot read property "childNodes" from null (http://live3.win007.com/common2.js#41)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot read property "childNodes" from null (http://live3.win007.com/common2.js#41)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:865)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:628)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:747)
    at com.gargoylesoftware.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:1032)
    at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:395)
    at com.gargoylesoftware.htmlunit.html.HtmlScript$3.execute(HtmlScript.java:276)

common2.js code:

function getOddsData() { 
        oddsHttp.open("get", "vbsxml/goalBf3.xml?r=007" + Date.parse(new Date()), false); 
        oddsHttp.setRequestHeader("User-Agent", ""); 
        oddsHttp.send(null); 
        var root = oddsHttp.responseXML.documentElement.childNodes[0];

oddsHttp as XMLHttpRequest

I suspect that a cross domain problem leads to " Cannot read property "childNodes""

I want to modify the JS by the following methods

public WebResponse getResponse(WebRequest request) throws IOException { 
  if(request.getUrl().toExternalForm().contains("common2.js")){ 
.... 
  } 
} 

How to fix it?


Solution

  • This is not a cross domain problem,the warning: Content is not allowed in prolog is the key

    I solved the problem by the following code

    new WebConnectionWrapper(wc) {
                             public WebResponse getResponse(WebRequest request) throws IOException {
                                    WebResponse response = super.getResponse(request);
    
                                    if(request.getUrl().toExternalForm().contains("goalBf3.xml")){
                                         System.out.println(response.getContentAsString("UTF-8"));
                                         String content = response.getContentAsString("UTF-8");
                                            if(null != content && !"".equals(content)){  
                                                if(content.indexOf("<") != -1 && content.lastIndexOf(">") != -1 && content.lastIndexOf(">") > content.indexOf("<"))  
                                                    content = content.substring(content.indexOf("<"), content.lastIndexOf(">") + 1);  
                                            } 
    
                                         WebResponseData data = new WebResponseData(content.getBytes("UTF-8"),
                                                    response.getStatusCode(), response.getStatusMessage(), response.getResponseHeaders());
                                            response = new WebResponse(data, request, response.getLoadTime());
                                    }
                                    return response;
                                }
                         }