无法使用htmlunit获取Cookie。我做错了什么?
Main.java
import java.util.logging.Level;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;
public class Main implements UserAgent {
private String firstUrl;
private String proxy1 = "XXX.XXX.XX.XXX";
//SET BROWSER VERSION
BrowserVersion browserVersion = new BrowserVersion(applicationNameIE_10_a,
applicationVersionIE_10_a, userAgentIE_10_a, browserVersionNumericIE_10_a) {
private static final long serialVersionUID = 1L;
};
//create WebClient : (Browser Version, IP Address, Port)
WebClient webC = new WebClient(browserVersion, proxy1, XXXX);
public Main (String url) throws Exception {
firstUrl = url;
}
public static void main (String args[]) throws Exception {
//URL URL URL URL URL URL URL URL URL URL URL URL URL
Main func1 = new Main ("http://whatsmyip.org/");
func1.function1();
}
//CREATES SINGLE BROWSER INSTANCE (Assigned to Specific Proxy IP Address)
public void function1() throws Exception {
//turn off ALL javascript/css errors!!!
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
HtmlPage pageResult1 = (HtmlPage) webC.getPage(firstUrl);
//System.out.println(pageResult1.asXml());
//System.out.println(pageResult1.asText());
//GETS IP ADDRESS FOR PROXY
DomNodeList<DomElement> headings = pageResult1.getElementsByTagName("h1");
for (DomElement e : headings) {
String varTest = e.getFirstElementChild().getTextContent();
System.out.println(varTest + " @ " + proxy1);
if(varTest.equals(proxy1)) {
System.out.println("Proxy Successfully Matched");
} else {
System.out.println("Proxy NOT MATCHED!!!");
}
}
webC.closeAllWindows();
}
public String getCookieValue(String cookieName) {
Cookie cookie = webC.getCookieManager().getCookie(cookieName);
if(cookie != null){
return cookie.getValue();
}
return "nothing";
}
}
UserAgent.java
public interface UserAgent {
//IE 10.0
String applicationNameIE_10_a = "Internet Explorer 10.0";
String applicationVersionIE_10_a = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
String userAgentIE_10_a = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
float browserVersionNumericIE_10_a = 10.0f;
}