browser extracts the “scheme”/protocol (we have established that this will be HTTP), host (www.example.com), and optional port number, resource path, and query strings
Now that the browser has the intended hostname for the request, it needs to resolve an IP address1. The browser will then look through its own cache of recently requested URLs, the operating system’s cache of recent queries, your router’s cache, and your DNS cache.
Much of this is done very simply, using what’s known as a sequence number for every byte sent. This allows the receiver to re-order received packets back into their original order, and allows the sender to retransmit any packet that does not get acknowledged by the receiver.
The HttpUrlConnection class allows us to perform basic HTTP requests without the use of any additional libraries. All the classes that we need are part of the java.net package.
The disadvantages of using this method are that the code can be more cumbersome than other HTTP libraries and that it does not provide more advanced functionalities such as dedicated methods for adding headers or authentication.
URL url = new URL(“http://example.com”); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod(“GET”);
Map< String, String > parameters = new HashMap<>(); parameters.put(“param1”, “val”); con.setDoOutput(true); DataOutputStream out = new DataOutputStream(con.getOutputStream()); out.writeBytes(ParameterStringBuilder.getParamsString(parameters)); out.flush(); out.close();
con.setRequestProperty(“Content-Type”, “application/json”);
String contentType = con.getHeaderField(“Content-Type”);
con.setConnectTimeout(5000); con.setReadTimeout(5000);
String cookiesHeader = con.getHeaderField(“Set-Cookie”); List< HttpCookie > cookies = HttpCookie.parse(cookiesHeader);
cookies.forEach(cookie -> cookieManager.getCookieStore().add(null, cookie));
Optional< HttpCookie > usernameCookie = cookies.stream() .findAny().filter(cookie -> cookie.getName().equals(“username”)); if (usernameCookie == null) { cookieManager.getCookieStore().add(null, new HttpCookie(“username”, “john”));}
con.disconnect(); con = (HttpURLConnection) url.openConnection(); con.setRequestProperty(“Cookie”,StringUtils.join(cookieManager.getCookieStore().getCookies(), “;”));
con.setInstanceFollowRedirects(false);
HttpUrlConnection.setFollowRedirects(false);
int status = con.getResponseCode();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer content = new StringBuffer(); while ((inputLine = in.readLine()) != null) {content.append(inputLine);} in.close();
con.disconnect();
int status = con.getResponseCode(); Reader streamReader = null; if (status > 299) {streamReader = new InputStreamReader(con.getErrorStream());} else {streamReader = new InputStreamReader(con.getInputStream());}
we can build it using some of the methods that the HttpUrlConnection instance offers:
StringBuilder fullResponseBuilder = new StringBuilder();