3.1 Badan Penerbit
Muga-muga sampeyan ora lali yen saliyane panjaluk GET , ana uga panjaluk PUT lan POST , nalika sampeyan uga kudu nambah panyuwunan , yaiku badan panyuwunan.request body
HttpRequest
Kelas kasebut nduweni kelas batin khusus kanggo iki BodyPublisher
. Sanajan sacara teknis minangka antarmuka sing duwe pirang-pirang implementasine, sing bakal dibahas ing ngisor iki
Lan kita bakal miwiti kanthi paling gampang - ora ana badan panyuwunan. Ya, mengkono.
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://codegym.cc"))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
Prasaja lan ayu.
3.2 sakaString()
Opsi paling umum kaloro yaiku ngliwati sawetara senar minangka badan panyuwunan. Iki ditindakake kanthi gampang:
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://codegym.cc"))
.headers("Content-Type", "text/plain;charset=UTF-8")
.POST(HttpRequest.BodyPublishers.ofString("Hello"))
.build();
Miturut cara, sampeyan bisa nyetel enkoding senar sing dikirim. Bisa migunani banget yen server http sing dikirim panjalukan ora bisa digunakake ing UTF8.
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://codegym.cc"))
.POST(HttpRequest.BodyPublishers.ofString("Hello", Charset. US-ASCII)))
.build();
3.3 saka File()
Pungkasan, sampeyan bisa uga pengin masang file menyang panyuwunan POST . Iki carane avatar sampeyan biasane diunggah menyang server. Kanggo nindakake iki, sampeyan kudu nelpon cara ofFile()
, ngendi kanggo nransfer Path
file lokal:
Path avatar = Path.of("c://avatar.jpeg");
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://codegym.cc"))
.headers("Content-Type", "image/jpeg")
.POST(HttpRequest.BodyPublishers.ofFile(avatar))
.build();
3.4 sakaByteArray()
Skenario umum liyane yaiku ngirim set bita menyang server. Contone, sampeyan nggawe serial sawetara obyek minangka sakumpulan bita, ndhelik soko, utawa mung pengin ngirim sawetara buffer data. Iki mbutuhake cara .ofByteArray()
.
Cara iki njupuk array byte minangka parameter. Tuladha:
byte[] data = "My Secret Message".getBytes();
byte[] dataEncripted = SuperEncriptor.encript(data);
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://codegym.cc"))
.headers("Content-Type", "application/octet-stream")
.POST(HttpRequest.BodyPublishers.ofByteArray(dataEncripted))
.build();
3.5 sakaInputStream()
Pungkasan, skenario pungkasan nanging paling menarik yaiku dilampirake InputStream
menyang panyuwunan POST .
Ana fungsi khusus kanggo iki ofInputStream()
. Lan dheweke pinter banget. Ngidini sampeyan ngatur transfer data saka stream menyang stream, masang stream data menyang panyuwunan POS sing durung dibukak.
Sampeyan kudu ngirim fungsi menyang fungsi ofInputStream()
, sing bakal ngasilake stream minangka asil InputStream
.
Tuladha:
byte[] data = "My Secret Message".getBytes();
//wrapping an array of bytes into a stream.
InputStream is = new ByteArrayInputStream(data);
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://codegym.cc"))
.headers("Content-Type", "application/octet-stream")
.POST(HttpRequest.BodyPublishers.ofInputStream (() -> is;))
.build();
Aku ora keganggu kanggo pager taman kene, nanging aku sampeyan ngerti prinsip. Yagene padha nindakake iku? Sampeyan meh tansah bisa nggunakake ofByteArray()
. Nanging yen sampeyan pengin ngirim data asynchronously utawa sampeyan kudu ngleksanakake sawetara skenario utamané Komplek, sampeyan bisa nggunakake cara iki.