1.1 Pambuka kanggo HttpClient

Diwiwiti karo JDK 11, pangembang platform Java nambahake alat anyar sing kuat menyang JDK kanggo nggawe panjalukan http, java.net.http. Isine papat kelas utama:

  • HttpClient
  • HttpRequest
  • HttpResponse
  • soket web

Iki minangka kelas sing kuat banget sing ngidini sampeyan nindakake kabeh jinis panjalukan kanthi nggunakake HTTP, HTTP/2lan WebSocket.

Kajaba iku, sampeyan bisa nggunakake kelas iki kanggo nggawe panjalukan http sinkron lan asinkron.

Nggawe panjalukan http kalebu bagean ing ngisor iki:

  1. Nggawe obyekHttpClient
  2. Nggawe obyekHttpRequest
  3. Ngirim panjalukan nggunakake cara send()utawasendAsync()
  4. Pangolahan responHttpResponse

Conto panyuwunan kasebut:


 HttpClient client = HttpClient.newBuilder()
        .version(Version.HTTP_1_1)
        .followRedirects(Redirect.NORMAL)
        .connectTimeout(Duration.ofSeconds(20))
        .proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
        .authenticator(Authenticator.getDefault())
        .build();
 
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body()); 

1.2 Pendekatan deklaratif

Ing conto ing ndhuwur, sampeyan ndeleng conto sing diarani pendekatan deklaratif kanggo nulis kode. Ayo ndeleng bagean pisanan saka conto:


 HttpClient client = HttpClient.newBuilder()
.version(Version.HTTP_1_1)
.followRedirects(Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
.proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
.authenticator(Authenticator.getDefault())
.build();

Kaya apa kode iki ditulis nganggo gaya klasik:


HttpClient client = HttpClient.new();
client.setVersion(Version.HTTP_1_1);
client.setFollowRedirects(Redirect.NORMAL);
client.setConnectTimeout(Duration.ofSeconds(20));
client.setProxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)));
client.setAuthenticator(Authenticator.getDefault());

Nalika nggunakake pendekatan deklaratif ing kode, rong perkara diganti. Kaping pisanan , kabeh metode kelas HttpClient ngasilake obyek dhewe , sing ngidini sampeyan ngatur kode ing bentuk rantai.

Kode klasik:

HttpClient client = HttpClient.new();
client.setVersion(Version.HTTP_1_1);
client.setFollowRedirects(Redirect.NORMAL);
client.setConnectTimeout(Duration.ofSeconds(20));
client.setAuthenticator(Authenticator.getDefault());
Minangka rantai:

HttpClient client = HttpClient.new() .setVersion(Version.HTTP_1_1) .setFollowRedirects(Redirect.NORMAL). setConnectTimeout(Duration.ofSeconds(20)) .setAuthenticator(Authenticator.getDefault());
Kita nransfer saben cara menyang baris sing kapisah (iki minangka pernyataan sing dawa)

HttpClient client = HttpClient.new()
.setVersion(Version.HTTP_1_1)
.setFollowRedirects(Redirect.NORMAL)
.setConnectTimeout(Duration.ofSeconds(20))
.setAuthenticator(Authenticator.getDefault());

Kapindho , awalan dibusak saka metode set, sing ngidini sampeyan nulis kode kanthi luwih kompak:

Was

HttpClient client = HttpClient.new()
.setVersion(Version.HTTP_1_1)
.setFollowRedirects(Redirect.NORMAL)
.setConnectTimeout(Duration.ofSeconds(20))
.setAuthenticator(Authenticator.getDefault());

Iku dadi

HttpClient client = HttpClient.new()
.version(Version.HTTP_1_1)
.followRedirects(Redirect.NORMAL)
.connectTimeout(Duration.ofSeconds(20))
.authenticator(Authenticator.getDefault());
    

Kode kasebut luwih gampang diwaca, sanajan luwih angel ditulis.

Lan siji liyane penting. Ing conto iki, pola Builder digunakake. Ana skenario ing ngendi nggawe obyek minangka proses sing rumit. Mulane, dheweke luwih seneng nggawe formal: diwiwiti kanthi panggilan metode kondisional begin()lan diakhiri karo panggilan metode kondisional end().

Ing conto sing dianalisis, metode kasebut HttpClient.newBuilder()ngasilake obyek HttpClient.Builder(iki minangka kelas sarana internal saka kelas HttpClient). Kabeh cara saka jinis version()diarani mung ing obyek layanan iki. Inggih, telpon saka cara build()nandhani pungkasan construction saka obyek lan ngasilake obyek HttpClient.