Java Magazine, July/August 2017
ORACLE COM JAVAMAGAZINE JULY AUGUST 2017 40 java 9 provide full compatibility with JDK 9 you can use the code samples in a Java application built with any IDE The following command launches JShell with the jdk incubator httpclient module specified as a value for the add modules option This way JShell will resolve the jdk incubator httpclient and you will be able to use it within the JShell session If you have multiple JDK versions installed and you dont have JDK 9 in your path you need to run the command in the bin folder for JDK 9 In macOS or Linux you might need to replace jshell with jshell jshell add modules jdk incubator httpclient JShell doesnt require you to use semicolons at the end of the statements However in order to make the code compatible with the Java code for building real life applications I prefer to use semicolons Enter the following import statement in JShell import jdk incubator http JShell includes many import statements by default However if you dont work with JShell you need to add the following additional import statements for the sample code to work import java lang import java net URI import java net URISyntaxException The new module separates requests from responses The following are the principal classes with which I work in the sample code to perform HTTP operations HttpClient Represents an immutable HTTP client with a specific configuration and allows you to send requests and receive responses HttpRequest Represents an HTTP request HttpResponse Represents an HTTP response The API provides builders to create instances and configure the diferent pieces These are static methods that start with the prefix new However the API doesnt provide builders for the headers represented with the HTTPHeader class Unfortunately the URI is still specified as a java net URI instance Thus if you need to use query parameters it is necessary to format or concatenate strings The following lines build an HttpClient instance named client by chaining many method calls after the call to the HttpClient newBuilder method that creates a new HttpClient builder For example the call to the followRedirects method with HttpClient Redirect ALWAYS as an argument specifies that I want the client to always follow redirects In this case I want to follow redirects because I want to perform an HTTP GET request to the following URI http www oracle com I do not specify any desired HTTP version for the client therefore the client will be created as an HTTP 11 client that wont request an upgrade to HTTP 2 in the request header The final chained call to the build method finishes the code for the builder HttpClient client HttpClient newBuilder followRedirects HttpClient Redirect ALWAYS build System out println client version URI uri new URI http www oracle com HttpRequest request HttpRequest newBuilder uri uri GET build After the HttpClient instance is built I print the result of calling the client version method JShell displays HTTP_ 1_ 1 because I used the default configuration Its default
You must have JavaScript enabled to view digital editions.