如何在HtmlUnit中解决跨域

时间:2016-06-21 09:17:28

标签: cross-domain htmlunit

错误:

六月 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代码:

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

我怀疑跨域问题会导致&#34;无法读取属性&#34; childNodes&#34;&#34;

我想通过以下方法修改JS

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

如何解决?

1 个答案:

答案 0 :(得分:0)

这不是跨域问题,警告: prolog中不允许内容是关键

我通过以下代码

解决了这个问题
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;
                            }
                     }