จำกัดขอบเขตข้อมูลรับรองพร็อกซีของ HtmlUnit ให้อยู่ที่โฮสต์พร็อกซี
กำหนดค่าพร็อกซี HtmlUnit และใช้ขอบเขต host/port ของผู้ให้ข้อมูลรับรอง พร้อมฟิลด์ domain และ workstation สำหรับ NTLM
การกำหนดค่าพร็อกซีของ HtmlUnit เป็นตัวเลือกตัวกลาง และผู้ให้ข้อมูลรับรองจะจัดหาบัญชีสำหรับโฮสต์และพอร์ตนั้น สำหรับพร็อกซีแบบ NTLM API ของ HtmlUnit 4.21.0 มีเมธอดเฉพาะ:
String proxyHost = "proxy.example";
int proxyPort = 8080;
DefaultCredentialsProvider credentials = new DefaultCredentialsProvider();
credentials.addNTLMCredentials(
user, password.toCharArray(), proxyHost, proxyPort, workstation, domain);
try (WebClient browser = new WebClient()) {
browser.getOptions().setProxyConfig(
new ProxyConfig(proxyHost, proxyPort, "http"));
browser.setCredentialsProvider(credentials);
HtmlPage page = browser.getPage("https://www.example.com/");
}
คลาสเหล่านี้ใช้แพ็กเกจ org.htmlunit ปัจจุบัน โดย HtmlPage อยู่ใน org.htmlunit.html ระบุ workstation และ domain ของบัญชีพร็อกซีตามสภาพแวดล้อม เว็บไซต์ปลายทางไม่ใช่ฟิลด์ domain ของ NTLM
สำหรับรายการข้อมูลรับรองแบบ username/password ทั่วไป ผู้ให้บริการเดียวกันมี addCredentials ซึ่งรับ username รหัสผ่านแบบ char array, host, port และ realm ค่า realm เป็น null จะใช้กับทุก realm บน endpoint ที่จำกัดขอบเขตนั้น
ใช้ overload ที่ตรงกับรูปแบบการยืนยันตัวตนของพร็อกซีและ dependency ที่ติดตั้งจริง ตัวอย่าง HtmlUnit 2.x แบบเก่าใช้ชื่อแพ็กเกจต่างกันและอาจมีซิกเนเจอร์รหัสผ่านแบบ string โค้ดด้านบนใช้ API 4.21.0