Question: Java networking question. Here's attempt to answer the question that did not work. import java.io.IOException; import org.apache.http.client.fluent.Request; public class NetworkingLecture1 { // 5 points static
Java networking question. Here's attempt to answer the question that did not work.
import java.io.IOException;
import org.apache.http.client.fluent.Request;
public class NetworkingLecture1 {
// 5 points static String Q1() { // Send an HTTP GET request to http://fury.cse.buffalo.edu/api/lecture1 and return the server's response String url = "http://fury.cse.buffalo.edu/api/lecture1"; public static getRequest(){ String response = ""; try{ response = Request.Get(url).execute().returnContent().asString(); }catch(IOException ex){ ex.printStackTrace(); }
return response; } }
public static void main(String[] args) {
}
}
Here's some notes to help answer the question!!
The Library
https://hc.apache.org/
We will use Apache's HttpComponents library to help us make HTTP requests over the Internet. This library contains a multitude of classes to explore, though we will only need to use a few of these for our purposes in this course.
This library will be setup in the repository of any assignment for which it's required
HTTP GET
To make an HTTP GET request using the Apache HttpComponents library we will use the following method. This method can be used directly in all of your assignments. This is one case where you can cut and paste this code wherever you need it.
public static String getRequest(String url){ String response = ""; try{ response = Request.Get(url).execute().returnContent().asString(); }catch(IOException ex){ ex.printStackTrace(); } return response; } This method will use the library to connect to a url (link). The Request class from the library is used to send a GET request to the url and return the response as a String. As with files, connecting to a website can go wrong (ex. if an invalid link is used), so the method calls are wrapped in a try/catch block.
HTTP POST
Similar to the get method this method will send a POST request with the provided params to the provided url. The params String is the data to be sent to the server identified by the url. This method can be used directly in all of your assignments. This is one case where you can cut and paste this code wherever you need it.
public static String postRequest(String url, String params){ String response = ""; try{ response = Request.Post(url) .bodyString(params, ContentType.DEFAULT_TEXT) .execute().returnContent().asString(); }catch(IOException ex){ ex.printStackTrace(); } return response; } This method is written in much the same way as the get method with only a few differences. First, the method calls are on separate lines which is a matter of formatting. It can be easier to read chained method calls if they are on separate lines and this is still valid syntax. Second, there is a call to bodyString which adds our params to the POST request in the format expected by the server (The format used by HTTP).
Most of this code will be the same every time you use this method, but you must specify the content type to match the type of information being sent. When we start sending JSON strings in HTTP POST requests we will change this value to ContentType.APPLICATION_JSON. This will tell the server how to handle the request. If the wrong content type is used the request might not be handled as expected.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
