In: Computer Science
Project Name: URLEncoder Target Platform: Console Programming Language: C# A Uniform Resource Identifier (URI) (Links to an external site.) is a string of characters designed for unambiguous identification of resources and extensibility via the URI scheme. The most common form of URI is the Uniform Resource Locator (URL) (Links to an external site.), frequently referred to informally as a web address. A user has a need to generate URLs for files they are storing on a server. The purpose of this program is to generate the URL string so the user doesn’t have to type it out. The program asks the user for several pieces of information and then uses it to generate the URL string and presents it in the Console where the user can copy it. This is being done so the user doesn’t have to type out the URL and helps avoid mistakes from typing and from the use of invalid characters in a URL. The following is the format for a URL that is to be generated by the program. https://companyserver.com/content/[project_name]/files/[activity_name]/[activity_name]Report.pdf (Links to an external site.) The [project_name] and [activity_name] are placeholders for pieces of information that go in the string. The [ ] do not get included in the string. They surround an identifier for the piece of information and are not part of the content. So, if [project_name] is “DesignLab” and [activity_name] is “Furnishings” then the URL is to be https://companyserver.com/content/DesignLab/files/Furnishings/FurnishingsReport.pdf (Links to an external site.) There are a couple of rules that have to be adhered to when creating URLs. No spaces are allowed in URLs. So, if [project_name] is “Design Lab” it can’t be included as-is in the URL. Spaces in URLs must be replaced with “%20”. The URL can contain “Design%20Lab”. If a space is present in the user input it must be converted to “%20”. Note: spaces can also be replaced with a +, but for this assignment spaces are to be converted to “%20”. In addition to spaces, there are other characters that may not be placed in URLs. The HTML URL Encoding Reference (Links to an external site.) from w3schools shows how characters that aren’t allowed in a URL can be converted to a %[number] value that can be in the URL. control characters: these are ASCII coded characters 00–1F and 7F hexadecimal delimiter characters: < > # % and " If a control character appears in the content provided by the user, the user is to be given feedback that the input is invalid because it contains a control character and they are to be asked to provide a different input. If a delimiter appears in the content provided by the user, the delimiter character is to be converted into its %[number] URL encoded format. For example, < is to be encoded as “%3C” and > is to be encoded as “%3E”. The values for the other characters can be found in the documentation provided above. There are some other characters that are reserved for use in some parts of the URL. They may not be allowed in one part of the URL even if they are allowed in other parts. These characters are also to be converted to their URL encoded format using %[number]. The following characters are reserved within a query component, have special meaning within a URL, and are to be converted. ; / ? : @ & = + $ , The following list of characters are not disallowed, but they may cause problems. They should also be converted to their URL encoded format. { } | \ ^ [ ] ` The following is an example to illustrate the application of the rules. Project Name: Design Lab Activity Name: Network>Implementation Result URL: https://companyserver.com/content/Design%20Lab/files/Network%3EImplementation/Network%3EImplementationReport.pdf (Links to an external site.) Your application, when run, is to prompt the user for the Project Name and the Activity Name. If the input is invalid, the user should be re-prompted for the input with a feedback message. After the input is successfully received, the URL string is to be created based on the rules above and presented to the user in the Console. After the URL is presented, the user is to be prompted if they want to create another URL. If they do, prompt them again for input. If not, exit the program. Please do in visual studios 2019.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace URLValidityChecker
{
class Program
{
//set of all the characters which are valid
const string _validChars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~#[]@!'()*,;";
//set of all the characters which are invalid
const string _invalidChars = "/?:@&=+$<>#%{ }|\\^[]`
";
//final URL building template string
const string _urlPlaceholder =
"https://companyserver.com/content/{0}/files/{1}/{2}Report.pdf";
static void Main(string[] args)
{
Console.WriteLine("-----------URL VALIDITY
CHECKER------------");
Console.WriteLine("Please press X when programs ask for
continuation to exit");
//will run untill correct exit criteria is met which in or case is
empty of X
while (true)
{
Console.WriteLine("PLEASE ENTER THE PROJECT NAME");
//Take project name input from user
string prjctName = Console.ReadLine();
if (string.IsNullOrEmpty(prjctName))
{
InvalidMsgToUser();
continue;
}
//format the name as per the business logc and url's character
acceptance
prjctName = StrFormatter(prjctName);
Console.WriteLine("PLEASE ENTER THE ACTIVITY NAME");
//Take project name input from user
string actName = Console.ReadLine();
if (string.IsNullOrEmpty(actName))
{
InvalidMsgToUser();
continue;
}
//format the activity name as per the business logc and url's
character acceptance
actName = StrFormatter(actName);
Console.WriteLine("FINAL URL : ");
Console.WriteLine(string.Format(_urlPlaceholder,prjctName,actName,actName));
Console.WriteLine("WOULD YOU LIKE TO CONTINUE FOR ANOTHER URL OR
WANT TO QUIT(Press X)");
string usrInpt=Console.ReadLine();
if(string.IsNullOrEmpty(usrInpt) ||
usrInpt.ToLower().Trim()=="x")
{
Console.WriteLine("PROCESS WILL EXIT NOW");
break;
}
}
Console.ReadLine();
}
static void InvalidMsgToUser()
{
Console.WriteLine("EMPTY OR INVALID ENTRY WAS MADE, PLEASE TRY
AGAIN!");
}
/// <summary>
/// Convert the String to character url and check for it's presence
in
/// valid or invalid collection of string defined at the top
/// for invalid ones we are simply converting them to the
respective ASCII code and then getting
/// their hexadecimal values.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static string StrFormatter(string str)
{
char[] strArr = str.ToCharArray();
char[] validChrsArr = _validChars.ToCharArray();
char[] invalidChrArr = _invalidChars.ToCharArray();
string finalStr = string.Empty;
foreach (char item in strArr)
{
if (invalidChrArr.Contains(item))
{
int value = Convert.ToInt32(item);
//Get the hexadecimal string value using correct string
conversion overload
finalStr += string.Format("%{0}", value.ToString("X2"));
}
else if (validChrsArr.Contains(item))
finalStr += item;
}
return finalStr;
}
}
}