如何使用Java 11 HttpClient调出URI的小示例。 真的没有什么新鲜的。 只是为了让我记住一天。
import java.net.URI ;
import java.net.http.* ;
import java.net.http.HttpClient.Redirect ;
import java.net.http.HttpClient.Version ;
import java.time.Duration ;
public class Junk {
public static void main ( String [] args ) throws Exception {
HttpClient client = HttpClient . newBuilder ()
. version ( Version . HTTP_1_1 )
. followRedirects ( Redirect . NORMAL )
. connectTimeout ( Duration . ofSeconds ( 20 ))
. build ();
HttpRequest request = HttpRequest . newBuilder ()
. uri ( URI . create ( "https://crossbrowsertesting.com" ))
. build ();
HttpResponse response = client . send ( request ,
HttpResponse . BodyHandlers . ofString ());
System . out . println ( response );
System . out . println ( response . body ());
}
}
Java文档在这里: https : //docs.oracle.com/zh-CN/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.html
翻译自: https://dev.to/tonetheman/jdk11-httpclient-example-1hnf