Question: Part 1 . URL Parsing ( 1 0 Marks ) Web URL s are made of many parts. Take the following url for example: https:
Part URL Parsing Marks
Web URLs are made of many parts. Take the following url for example:
https:acsuwinnipeg.casearchhelloworld&x
We have the following parts:
Scheme: https
subdomain: acs
domain: uwinnipeg
toplevel domain: ca
subdirectory or route: search
query parameters: everything following the
The query parameters are variables in the url that follow
a These parameters are values that get passed to a webpage, like we would pass variables to a Java method. The parameter name and value are always separated by a while different
parameters are separated by &s For the sample url we have two parameters: hello world,
and x
make the following changes in the java file given below :
a Marks Complete the parseParams method. This method will accept a url String as
input, and the parse out all of the parameters from the url. The parameters should be
returned as a D array of Strings. Where the array holds the pairs or parameter names and
values. Use the split method from the String class to answer this question.
eg: Our example url would return helloworldx
b Mark URLs will often need to encode many special characters in the query params. If
youve ever noticed urls with things like helloworld, this is why. Use the decode
method from the URLDecoder class to decode your urls in the parseParams method.
c Mark A url should contain at most one unencoded Include validation in your
parseParams method to check that the url argument is valid.
d Marks Complete the buildQuery method. This method will take a D array of Strings
that represent query params. This array is the same format as the output of part a Your
method will return the rebuilt query part of the url. You must use StringBuilder to rebuild
your result.
For example helloworldx would return helloworld&x
e Marks As mentioned, urls often need to encode special characters in their query
parameters. Use the URLEncoder class to ensure that all of your values are encoded while
building the query again.
Java file:
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
public class Lab
public static void mainString args
A list of various urls
String completeUrls
https:githubcomsearchqmachinelearning&typerepositories&lPython",
https:githubcomsearchqstringbuilder&typediscussions",
https:githubcomsearchqdateparserlanguageAJavaScriptcreatedAE&typerepositories",
https:githubcom
https:wwwgoogle.comsearchqjavadocsstringbuilder",
https:wwwuwinnipeg.casearchresults.htmlqappliedcomputerscience",
https:wwwbestbuy.caencasearchsearchcoolcomputerstuff&sorthighestRated",
https:docsoracle.comsearchqstringbuilder&pg&size&productenFjavaFjavaseF&categoryenFjava&showfirstpagetrue&cTypeWM&langen
https:wwwyoutube.comwatchvdQwwWgXcQ
https:wwwgoogle.comsearchqshouldthrowexception?toomanyquestionmarks",
;
System.out.printlnParsing params...";
forString url : completeUrls
System.out.printlnurl;
This trycatch block will stop our code from crashing if an IllegalArgumentException occurs
We'll learn more about these trycatch blocks later in the course :
try
Parse the URL to get the query parameters
String params parseParamsurl;
System.out.printlntParams: Arrays.deepToStringparams;
Using the query params, rebuild the query from the URL
String rebuiltQuery buildQueryparams;
System.out.printlntRebuilt: rebuiltQuery;
catchIllegalArgumentException e
If illegal argument exception occurs, print it out
System.out.printlntException: e;
System.out.print
;
public static String parseParamsString url
Hint is a special character!
Your code goes here
public static String buildQueryString params
Your code goes here
Use StringBuilder
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
