Apache HttpClient - การตรวจสอบสิทธิ์พร็อกซี
ในบทนี้เราจะเรียนรู้วิธีการสร้าง HttpRequest ที่พิสูจน์ตัวตนโดยใช้ชื่อผู้ใช้และรหัสผ่านและสร้างช่องสัญญาณผ่านพร็อกซีไปยังโฮสต์เป้าหมายโดยใช้ตัวอย่าง
ขั้นตอนที่ 1 - สร้างวัตถุ CredentialsProvider
CredentialsProvider Interface เก็บรักษาคอลเลกชันเพื่อเก็บข้อมูลรับรองการล็อกอินของผู้ใช้ คุณสามารถสร้างอ็อบเจ็กต์ได้โดยสร้างอินสแตนซ์คลาส BasicCredentialsProvider ซึ่งเป็นการใช้งานอินเทอร์เฟซนี้โดยดีฟอลต์
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
ขั้นตอนที่ 2 - ตั้งค่าข้อมูลรับรอง
คุณสามารถตั้งค่าหนังสือรับรองที่จำเป็นให้กับอ็อบเจ็กต์ CredentialsProvider โดยใช้ไฟล์ setCredentials()วิธี. วิธีนี้ยอมรับสองวัตถุ -
AuthScope object - ขอบเขตการตรวจสอบความถูกต้องระบุรายละเอียดเช่นชื่อโฮสต์หมายเลขพอร์ตและชื่อโครงร่างการพิสูจน์ตัวตน
Credentials object- การระบุข้อมูลประจำตัว (ชื่อผู้ใช้รหัสผ่าน) ตั้งค่าข้อมูลรับรองโดยใช้setCredentials() วิธีการสำหรับทั้งโฮสต์และพร็อกซีดังที่แสดงด้านล่าง
credsProvider.setCredentials(new AuthScope("example.com", 80), new
UsernamePasswordCredentials("user", "mypass"));
credsProvider.setCredentials(new AuthScope("localhost", 8000), new
UsernamePasswordCredentials("abc", "passwd"));
ขั้นตอนที่ 3 - สร้างวัตถุ HttpClientBuilder
สร้างไฟล์ HttpClientBuilder ใช้ custom() วิธีการของ HttpClients class ดังรูปด้านล่าง -
//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();
ขั้นตอนที่ 4 - ตั้งค่า CredentialsProvider
คุณสามารถตั้งค่าวัตถุ CredentialsProvider เป็นวัตถุ HttpClientBuilder โดยใช้ไฟล์ setDefaultCredentialsProvider()วิธี. ส่งผ่านไฟล์CredentialsProvider คัดค้านวิธีนี้
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
ขั้นตอนที่ 5 - สร้าง CloseableHttpClient
สร้างไฟล์ CloseableHttpClient วัตถุโดยใช้ build() วิธี.
CloseableHttpClient httpclient = clientbuilder.build();
ขั้นตอนที่ 6 - สร้างพร็อกซีและโฮสต์เป้าหมาย
สร้างโฮสต์เป้าหมายและพร็อกซีโดยสร้างอินสแตนซ์ไฟล์ HttpHost ชั้นเรียน
//Creating the target and proxy hosts
HttpHost target = new HttpHost("example.com", 80, "http");
HttpHost proxy = new HttpHost("localhost", 8000, "http");
ขั้นตอนที่ 7 - ตั้งค่าพร็อกซีและสร้างวัตถุ RequestConfig
สร้างไฟล์ RequestConfig.Builder วัตถุโดยใช้ custom()วิธี. ตั้งค่าวัตถุ proxyHost ที่สร้างไว้ก่อนหน้านี้เป็นRequestConfig.Builder ใช้ setProxy()วิธี. สุดท้ายสร้างไฟล์RequestConfig วัตถุโดยใช้ build() วิธี.
RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
RequestConfig config = reqconfigconbuilder.build();
ขั้นตอนที่ 8 - สร้างวัตถุคำขอ HttpGet และตั้งค่าวัตถุกำหนดค่าเป็น
สร้างไฟล์ HttpGetวัตถุโดยการสร้างอินสแตนซ์คลาส HttpGet ตั้งค่าวัตถุกำหนดค่าที่สร้างในขั้นตอนก่อนหน้าเป็นวัตถุนี้โดยใช้setConfig() วิธี.
//Create the HttpGet request object
HttpGet httpGet = new HttpGet("/");
//Setting the config to the request
httpget.setConfig(config);
ขั้นตอนที่ 9 - ดำเนินการตามคำขอ
ดำเนินการตามคำขอโดยส่งผ่านวัตถุ HttpHost (เป้าหมาย) และคำขอ (HttpGet) เป็นพารามิเตอร์ไปยัง execute() วิธี.
HttpResponse httpResponse = httpclient.execute(targetHost, httpget);
ตัวอย่าง
ตัวอย่างต่อไปนี้สาธิตวิธีดำเนินการคำขอ HTTP ผ่านพร็อกซีโดยใช้ชื่อผู้ใช้และรหัสผ่าน
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
public class ProxyAuthenticationExample {
public static void main(String[] args) throws Exception {
//Creating the CredentialsProvider object
CredentialsProvider credsProvider = new BasicCredentialsProvider();
//Setting the credentials
credsProvider.setCredentials(new AuthScope("example.com", 80),
new UsernamePasswordCredentials("user", "mypass"));
credsProvider.setCredentials(new AuthScope("localhost", 8000),
new UsernamePasswordCredentials("abc", "passwd"));
//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();
//Setting the credentials
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
//Building the CloseableHttpClient object
CloseableHttpClient httpclient = clientbuilder.build();
//Create the target and proxy hosts
HttpHost targetHost = new HttpHost("example.com", 80, "http");
HttpHost proxyHost = new HttpHost("localhost", 8000, "http");
//Setting the proxy
RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
RequestConfig config = reqconfigconbuilder.build();
//Create the HttpGet request object
HttpGet httpget = new HttpGet("/");
//Setting the config to the request
httpget.setConfig(config);
//Printing the status line
HttpResponse response = httpclient.execute(targetHost, httpget);
System.out.println(response.getStatusLine());
}
}
เอาต์พุต
ในการดำเนินการโปรแกรมด้านบนจะสร้างผลลัพธ์ต่อไปนี้ -
HTTP/1.1 200 OK