2017-04-01 12 views
0

Javaプログラムから実行できるブラウザを使用する必要があり、カスタムHTTPリクエストを直接送信してHTTPリクエストの一部を変更することができます。したがって、私はHtmlUnitを選択しました。HtmlUnit 400追加のホストヘッダーが設定されているときにリクエストが遅くなる

http://localhostに簡単なGETリクエストを行い、追加のヘッダHost:localhostを追加しようとしました。私のlocalhostホームのページはXAMPP homeです。ページ内に、他のサイト(facebook.com)へのリクエストがあります。ホストヘッダーが追加された場合、私は400 Bad Request errorを得ました。ホストヘッダーを追加しないと、エラーは発生しません。 HtmlUnitで

com.gargoylesoftware.htmlunit.ScriptException: Wrapped com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 400 Bad Request for http://connect.facebook.net/en_US/all.js#xfbml=1&appId=277385395761685 
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:894) 
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:628) 
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:515) 
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:775) 
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:751) 
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:739) 
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptIfPossible(HtmlPage.java:910) 
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeInlineScriptIfNeeded(HtmlScript.java:307) 
at com.gargoylesoftware.htmlunit.html.HtmlScript.executeScriptIfNeeded(HtmlScript.java:368) 
at com.gargoylesoftware.htmlunit.html.HtmlScript$2.execute(HtmlScript.java:239) 
at com.gargoylesoftware.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:258) 
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:781) 
at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source) 
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.endElement(HTMLParser.java:738) 
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1243) 
at net.sourceforge.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1143) 
at net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:226) 
at net.sourceforge.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:345) 
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:3154) 
at net.sourceforge.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:2117) 
at net.sourceforge.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:945) 
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:521) 
at net.sourceforge.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:472) 
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) 
at com.gargoylesoftware.htmlunit.html.HTMLParser$HtmlUnitDOMBuilder.parse(HTMLParser.java:988) 
at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:246) 
at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:188) 
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:267) 
at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:155) 
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:517) 
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:391) 
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:309) 
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:473) 

、すべての追加のヘッダーは、ページ上のすべての要求(最初のURLだけではなく)に適用されていますか? 修正方法?

+0

http://stackoverflow.com/help/someone-answers –

答えて

0

追加のリクエストは他のリクエストには適用されません。

のindex.html:

First 
<a href="index2.html">test</a> 

index2.html

Second 

テストケース:

LogManager.getLogger("org.apache.http.wire").setLevel(Level.ALL); 
final CollectingAlertHandler handler = new CollectingAlertHandler(); 
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) { 
    WebRequest request = new WebRequest(new URL("http://localhost:8080")); 
    request.setAdditionalHeader("Host", "abc.com"); 
    HtmlPage page = webClient.getPage(request); 
    page.getAnchors().get(0).click(); 
} 

以下

はあなたとそれを比較し、完全なケースです

出力:

DEBUG 08:07:31,868 org.apache.http.wire: http-outgoing-0 >> "GET/HTTP/1.1[\r][\n]" 
DEBUG 08:07:31,869 org.apache.http.wire: http-outgoing-0 >> "Host: abc.com[\r][\n]" 
DEBUG 08:07:31,869 org.apache.http.wire: http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]" 
DEBUG 08:07:31,869 org.apache.http.wire: http-outgoing-0 >> "Accept: */*[\r][\n]" 
DEBUG 08:07:31,869 org.apache.http.wire: http-outgoing-0 >> "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36[\r][\n]" 
DEBUG 08:07:31,869 org.apache.http.wire: http-outgoing-0 >> "Accept-Encoding: gzip, deflate[\r][\n]" 
DEBUG 08:07:31,869 org.apache.http.wire: http-outgoing-0 >> "Accept-Language: en-US[\r][\n]" 
DEBUG 08:07:31,870 org.apache.http.wire: http-outgoing-0 >> "[\r][\n]" 
DEBUG 08:07:31,873 org.apache.http.wire: http-outgoing-0 << "HTTP/1.1 200 [\r][\n]" 
DEBUG 08:07:31,873 org.apache.http.wire: http-outgoing-0 << "Accept-Ranges: bytes[\r][\n]" 
DEBUG 08:07:31,873 org.apache.http.wire: http-outgoing-0 << "ETag: W/"37-1491113233075"[\r][\n]" 
DEBUG 08:07:31,873 org.apache.http.wire: http-outgoing-0 << "Last-Modified: Sun, 02 Apr 2017 06:07:13 GMT[\r][\n]" 
DEBUG 08:07:31,873 org.apache.http.wire: http-outgoing-0 << "Content-Type: text/html[\r][\n]" 
DEBUG 08:07:31,873 org.apache.http.wire: http-outgoing-0 << "Content-Length: 37[\r][\n]" 
DEBUG 08:07:31,875 org.apache.http.wire: http-outgoing-0 << "Date: Sun, 02 Apr 2017 06:07:31 GMT[\r][\n]" 
DEBUG 08:07:31,875 org.apache.http.wire: http-outgoing-0 << "[\r][\n]" 
DEBUG 08:07:31,875 org.apache.http.wire: http-outgoing-0 << "First[\r][\n]" 
DEBUG 08:07:31,875 org.apache.http.wire: http-outgoing-0 << "<a href="index2.html">test</a>" 
DEBUG 08:07:32,172 org.apache.http.wire: http-outgoing-0 >> "GET /index2.html HTTP/1.1[\r][\n]" 
DEBUG 08:07:32,172 org.apache.http.wire: http-outgoing-0 >> "Host: localhost:8080[\r][\n]" 
DEBUG 08:07:32,172 org.apache.http.wire: http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]" 
DEBUG 08:07:32,172 org.apache.http.wire: http-outgoing-0 >> "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8[\r][\n]" 
DEBUG 08:07:32,172 org.apache.http.wire: http-outgoing-0 >> "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36[\r][\n]" 
DEBUG 08:07:32,173 org.apache.http.wire: http-outgoing-0 >> "Referer: http://localhost:8080/[\r][\n]" 
DEBUG 08:07:32,173 org.apache.http.wire: http-outgoing-0 >> "Accept-Encoding: gzip, deflate[\r][\n]" 
DEBUG 08:07:32,173 org.apache.http.wire: http-outgoing-0 >> "Accept-Language: en-US[\r][\n]" 
DEBUG 08:07:32,173 org.apache.http.wire: http-outgoing-0 >> "[\r][\n]" 
DEBUG 08:07:32,174 org.apache.http.wire: http-outgoing-0 << "HTTP/1.1 200 [\r][\n]" 
DEBUG 08:07:32,174 org.apache.http.wire: http-outgoing-0 << "Accept-Ranges: bytes[\r][\n]" 
DEBUG 08:07:32,174 org.apache.http.wire: http-outgoing-0 << "ETag: W/"6-1491113144011"[\r][\n]" 
DEBUG 08:07:32,174 org.apache.http.wire: http-outgoing-0 << "Last-Modified: Sun, 02 Apr 2017 06:05:44 GMT[\r][\n]" 
DEBUG 08:07:32,174 org.apache.http.wire: http-outgoing-0 << "Content-Type: text/html[\r][\n]" 
DEBUG 08:07:32,175 org.apache.http.wire: http-outgoing-0 << "Content-Length: 6[\r][\n]" 
DEBUG 08:07:32,175 org.apache.http.wire: http-outgoing-0 << "Date: Sun, 02 Apr 2017 06:07:31 GMT[\r][\n]" 
DEBUG 08:07:32,175 org.apache.http.wire: http-outgoing-0 << "[\r][\n]" 
DEBUG 08:07:32,175 org.apache.http.wire: http-outgoing-0 << "Second" 
関連する問題