/**
* Simple example for a basic http client.
* We do not catch any exceptions.
* We throw all possible exceptions up the stack.
* @author Subpavee Duke Poompuang
*/package inetAddress;import java.io.*;
import java.net.*;public class HttpClientTester
{
private final static String URL
= "https://cisp21profh.pythonanywhere.com/api/spring2020"; public static void main(final String[] args) thros IOExecption
{
if(args.length > 0)
{
String host = args[0];
InetAddress[] addresses = InetAddress.getAllByName(host);
for(InetAddress a : addresses)
System.out println(a);
}
throws /* REPLACE THIS COMMENT WITH THE POSSIBLE EXCEPTIONS */
{
// WRITE A STATEMENT TO INITIALIZE A CLIENT NAMED client.
// THE CLIENT SHOULD USE THE DEFAULT CONFIGURATION.
// USE NO CHAINED METHOD CALLS.
// USE THE EXAMPLE IN THE CORE JAVA BOOK AS A GUIDE. /*** REPLACE THE COMMENTS IN THE NEXT LINE WITH YOUR CODE ***/
/* CLASS NAME */ client = /* INITIALIZATION CODE */ if (client != null)
{
System.out.println("\nclient object: " + client); // INITIALIZE AN OBJECT NAMED request FOR AN HTTP GET REQUEST.
// USE A CHAINED METHOD CALL LIKE THE EXAMPLE IN THE CORE JAVA BOOK.
// USE THE CONSTANT URL DEFINED IN THIS SOURCE CODE FILE. /*** REPLACE THE COMMENTS IN THE NEXT LINE WITH YOUR CODE ***/
/* CLASS NAME */ request = /* INITIALIZATION CODE */ if (request != null)
{
System.out.println("\nrequest object: " + request); // NOW, WE HAVE A CLIENT OBJECT AND A REQUEST OBJECT. // THE CLIENT NEEDS TO REQUEST A RESPONSE FROM THE SERVER.
// THIS IS A SIMPLE EXAMPLE, SO WE WILL DO IT SYNCHRONOUSLY.
// WE EXPECT THE SERVER TO RESPOND WITH A STRING. /*** REPLACE THE COMMENTS IN THE NEXT LINE WITH YOUR CODE ***/
/* CLASS NAME */ response = /* YOUR CODE */ // IF WE RECEIVED A RESPONSE FROM THE SERVER,
// WE'LL EXTRACT SOME DATA FROM IT.
if (response != null)
{
// IN THE HTTP PROTOCOL, THE STATUS CODE 200 IN THE RESPONSE MEANS
// THE REQUEST SUCCEEDED AND THE SERVER TRANSMITTED WHAT
// THE CLIENT REQUESTED IN THE RESPONSE BODY. // WE HAVE TWO CASES HERE,
// THE REQUEST SUCCEEDED OR NOT.
// SO WRITE A TWO-BRANCH STATEMENT. // THE FIRST BRANCH SHOULD PRINT
// THE FOLLOWING 4 PIECES OF INFORMATION:
// (1) THE RESPONSE BODY
// AND FROM THE RESPONSE HEADERS:
// (2) THE DATE, (3) CONTENT-TYPE, AND (4) CONTENT-LENGTH // THE SECOND BRANCH SHOULD PRINT
// AN ERROR MESSAGE THAT INDICATES THE STATUS CODE OF THE FAILURE. // TO BE CLEAR:
// WRITE AN IF/ELSE STATEMENT
// BASED ON A SUCCESSFUL STATUS CODE/***********************************************************************
* HERE...
* END EACH BLOCK WITH A PROPERLY INDENTED BRACE.
* AFTER EACH CLOSING BRACE, WRITE A VERY SHORT COMMENT THAT
* DESCRIBES THE BLOCK THAT IS BEING CLOSED, LIKE // end of main()
*
* THIS WAS FUN.
* WE CAN WRITE CODE THAT TALKS WITH A WEB SERVER
* AND PROCESSES ITS RESPONSES.
*
* BUT THIS IS MUCH EASIER IN PYTHON...
***********************************************************************/