This video we'll work with the Java 9 TTP to client in the Java shell.
So the first thing you'll see is I've opened up a Java shell in verbose mode and I just want to import
something so let's say import
JDK got incubator dot HTP dot star and you'll notice we've got a problem right away and the error is
very very good.
It says package JDK incubator age GTP is not visible.
Does this is not found.
This is not visible.
It tells you that the package is declared in DJT incubator HTP client module which is not in the module
graph.
So what we need to do is we need to add this module to the Java shell and we did that before.
If you do a slash end of the dash dat add modules followed by the module name in this case JDK Daut
incubator dot h TTP client that adds the module.
If you get an error here check your spelling because maybe you spelt something wrong.
Now the module has been added.
Now I'm going to hit the up arrow a couple of times.
Let's try to import the package.
Now we're good.
So now we've got the package.
So let's create an HTP client create a request object send it get a response back.
First thing we'll do is create a string called u r l and it'll be h TTP w w w died example dot com.
So there is a u r l.
So now let's create the HTP client.
So what we'll do is we'll say h t t p client and I'll use explicit names rather than having it create
scratch variables for me.
So we'll just call this client and we'll say this is HTP client and we'll call the static method new
or we can use the tab completion here.
So now we've got the GTP client created.
So now what we need to do is we need to create an age GTP request that we can send to the server through
the client.
OK.
So let's do that.
HTP request and we'll call it request and we'll say TTP request
and we'll use the builder.
So we'll say new builder.
Now here we need to create a you are I based on the you are all that we got.
So we'll say new you are.
I give it to you or else that we've got the example dot com.
OK.
So that's going to create the you or I then we'll do a get request and we'll build this.
It's as simple as that.
That's created the request and you can see here that request is a get request to w w w exampled outcome.
OK so now what we need to do is we need to send the request to the client and get back the response
so let's do that as well.
TTP response and again we'll call this response equals.
Now at this point we need to ask the client.
Remember the client is right up here that's the HTP client we created.
So we're going to tell the client send that request that we just created a second ago and now we need
to tell it how we're going to handle the responses body.
And there's a couple of different options you can use but we're just going to use HTP response dot body
händler dot and all tabs.
You can see some of the options.
Should have an H.
Sorry about that.
There we go.
So we can have as byte array as byte array consumers file is file download.
I'm just going to use as string.
We can also discard this sometimes we really don't care what's coming back in the body you're just interested
in headers and so forth.
So in this case I'm just going to say as strange.
And now I've got my response I'm going to clear the screen and look at the variables that I've created.
And you can see I've got a URL here I've got a client request and the request was sent via the client
and I've got a response.
So if I say response dot and press tab you can see some of the methods available to me.
Notice here I can get the responses body.
I can get the headers.
Let's take a look at the status quo.
That's pretty revealing and HGP.
So let's see what the status code is.
Hopefully it's two hundred.
It is.
That means it was a successful request.
So now let's look at the statuses sorry the responses body.
So let's look at response dot body.
Now this is where I would expect the h t m l and that's exactly what I've got back.
This is the H G M L for w w w dot example dot com.
OK that's pretty easy really easy to use within the Java shell the tricky piece is getting that module
in the beginning.
If you don't do that obviously none of this will work but the error message you get is really really
good because it gives you a clue as to what you need to do.
So let's see what else we can do here.
We've got the response so let's say response dot and see what else we've got.
Let's take a look at the headers over here.
OK.
So let's say age TTP headers and this is called header's is response that will get the headers from
the response.
OK so now we've got again I'll clear the screen and look at our variables.
So now I've got an object called Hetter that is basically all the headers that I'm getting back in the
response from the server.
Well how do I process that if I say Hetter dot.
You'll see there's one called all values this probably the one I want.
There's also another one called Map and that's the one I'm going to use.
The headers are basically key value pairs where the key is a string and the value is a list of strings.
OK so let me see how that works.
It's pretty easy to do.
Again clear the screen.
I'll look at the variables.
We have a clean screen here.
What I'll say is I want to create a map of string.
That's the key and the value is a list of string.
I'll just call it map and all that.
That Hetter dot map.
All right.
So I've got my map my key value pairs here.
If I just display it you can kind of see content type content length all the stuff you would expect
to see but it's kind of hard to read.
So let's just look through here and print each one of them out.
So I'll say map dot for each and we'll use the lambda here for each key value pair.
I'll just say system out.
Print line.
The key plus colon plus the value just so we could see them nicely.
And there you go you can see you've got typical headers that you would expect back for response.
What's the Content-Length.
It's a text h Jamelle file dates and so forth.
So that's it.
That concludes this video.
Now you know how to use the TTP to client within the Java shell.
Again that very first step is the one that could get you in but the error message is a great clue.
Once you add the module you're all set.