ตัวอย่าง Java14 Synchronous HttpClient – ภาพรวมและการสอนแบบง่าย – send()
เผยแพร่แล้ว: 2020-09-06 Java เพิ่งเปิดตัว Java 14
JDK ในบทช่วยสอนนี้ เราจะพูดถึงภาพรวมและการสอนไคลเอ็นต์ Java Synchronous HttpClient แบบง่าย
หากคุณมีคำถามใด ๆ ด้านล่างแสดงว่าคุณมาถูกที่แล้ว
- คุณจะสร้างคำขอ HTTP แบบอะซิงโครนัสใน JAVA . ได้อย่างไร
- เหตุใดจึงต้องใช้ HttpClient สำหรับการเชื่อมต่อแบบซิงโครนัส
- ข้อมูลเบื้องต้นเกี่ยวกับ HttpClient . ของ Java 11
- บทช่วยสอน Java 11 HTTP/2 API
- บทนำสู่ไคลเอ็นต์ Java HTTP OpenJDK
- ไคลเอนต์ Java HTTP – ตัวอย่างและสูตร
สามารถใช้ HttpClient
เพื่อส่งคำขอและรับการตอบกลับ HttpClient ถูกสร้างขึ้นผ่านตัวสร้าง
มาเริ่มกันเลย:
สร้างไฟล์ CrunchifyJavaSynchronousHTTPClient.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package crunchify . com . tutorial ; import java . io . IOException ; import java . net . URI ; import java . net . http . HttpClient ; import java . net . http . HttpHeaders ; import java . net . http . HttpRequest ; import java . net . http . HttpResponse ; import java . time . Duration ; /** * @author Crunchify.com * Overview and Simple Java Synchronous HttpClient Client Tutorial */ public class CrunchifyJavaSynchronousHTTPClient { private static final HttpClient crunchifyHttpClient = HttpClient . newBuilder ( ) . version ( HttpClient . Version . HTTP_1_1 ) . connectTimeout ( Duration . ofSeconds ( 5 ) ) . build ( ) ; // HttpClient: An HttpClient can be used to send requests and retrieve their responses. An HttpClient is created through a builder. // Duration: A time-based amount of time, such as '5 seconds'. // send() This class models a quantity or amount of time in terms of seconds and nanoseconds. It can be accessed using other duration-based units, such as minutes and hours. // In addition, the DAYS unit can be used and is treated as exactly equal to 24 hours, thus ignoring daylight savings effects. See Period for the date-based equivalent to this class. public static void main ( String [ ] args ) { HttpRequest crunchifyRequest = HttpRequest . newBuilder ( ) . GET ( ) . uri ( URI . create ( "https://crunchify.com/wp-content/java/crunchify-java-httpclient-tutorial.html" ) ) . setHeader ( "User-Agent" , "Crunchify Java Synchronous HTTPClient Example..." ) . build ( ) ; // HttpResponse: An HttpResponse is not created directly, but rather returned as a result of sending an HttpRequest. HttpResponse < String > crunchifyResponse = null ; try { // CompletableFuture: A Future that may be explicitly completed (setting its value and status), and may be used as a CompletionStage, supporting dependent functions and actions that trigger upon its completion. // send: Sends the given request using this client, blocking if necessary to get the response. // The returned HttpResponse<T> contains the response status, headers, and body ( as handled by given response body handler ). crunchifyResponse = crunchifyHttpClient . send ( crunchifyRequest , HttpResponse . BodyHandlers . ofString ( ) ) ; // print response headers HttpHeaders crunchifyHeaders = crunchifyResponse . headers ( ) ; crunchifyHeaders . map ( ) . forEach ( ( k , v ) - > System . out . println ( k + ":" + v ) ) ; // print status code crunnchifyPrint ( crunchifyResponse . statusCode ( ) ) ; // print response body crunnchifyPrint ( crunchifyResponse . body ( ) ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } } private static void crunnchifyPrint ( Object data ) { System . out . println ( data ) ; } } |
crunchifyHttpClient.send()
java API ส่งคำขอที่กำหนดโดยใช้ไคลเอนต์นี้ บล็อกหากจำเป็นเพื่อรับการตอบกลับ

HttpResponse<T>
ที่ส่งคืนมีสถานะการตอบสนอง ส่วนหัว และเนื้อหา (ตามที่จัดการโดยตัวจัดการเนื้อหาการตอบสนองที่กำหนด)
เพียงเรียกใช้โค้ดด้านบนเป็นโปรแกรม Java แล้วคุณจะเห็นการตอบสนองดังนี้
ผลลัพธ์ของคอนโซล:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/ Library / Java / JavaVirtualMachines / jdk - 14.0.2.jdk / Contents / Home / bin / java - javaagent : / Applications / IntelliJ IDEA . app crunchify . com . tutorial . CrunchifyJavaSynchronousHTTPClient accept - ranges : [ bytes ] connection : [ keep - alive ] content - length : [ 108 ] content - type : [ text / html ; charset = UTF - 8 ] date : [ Sun , 06 Sep 2020 02 : 02 : 58 GMT ] etag : [ "5f5442af-6c" ] last - modified : [ Sun , 06 Sep 2020 02 : 00 : 15 GMT ] server : [ nginx ] x - edge - location - klb : [ VhPBsSUriL2dZtW2Pu8FgAke45e082923ff37191eace6947ec35b35d ] 200 < ! DOCTYPE html > < html > < body > < h1 > Hey . . This is Crunchify ' s Java HTTPClient Tutorial . < / h1 > < / body > < / html > Process finished with exit code 0 |
โปรดแจ้งให้เราทราบหากคุณประสบปัญหาใดๆ ที่ทำงานอยู่เหนือโค้ด Java
อะไรต่อไป?
Java Asynchronous HttpClient ภาพรวมและการสอน – sendAsync()