So here is the code for my simple webserver simulation
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.Scanner;
import java.util.zip.GZIPInputStream;
public final class WebServer {
final class HTTPRequest {
final static String CRLF = "\r\n";
Socket socket;
public HTTPRequest(Socket socket)
{
this.socket = socket;
}
public void processRequest() throws IOException
{
InputStream clientIn = this.socket.getInputStream();
OutputStream clientOut = this.socket.getOutputStream();
BufferedReader clientReader = new BufferedReader(
new InputStreamReader(clientIn, Charset.forName("UTF-8")));
String requestLine = clientReader.readLine();
System.out.println(requestLine);
String headerLine = null;
while((headerLine = clientReader.readLine()) != null && headerLine.length() != 0)
System.out.println(headerLine);
clientOut.close();
clientReader.close();
this.socket.close();
}
}
private int portNumberErrorFix(int n)
{
Scanner in = new Scanner(System.in);
int result = 0;
switch(n)
{
case 0:
{
System.out.print("ERROR!!!\nThe port number given is out of the proper range(1024 > port number < 65535). Please enter a new Port Number:");
result = in.nextInt();
}
case 1:
{
System.out.print("ERROR!!!)\nThe port number given could not be used. Please enter a new port number as a number between 1024 and 65535:");
if(in.hasNextInt())
result = in.nextInt();
else
return 0;
}
}
if(result > 1024 && result < 65535)
return result;
else
return 0;
}
private int readArgs(String argument)
{
int portNumber = 0;
try{
portNumber = Integer.parseInt(argument);
while(portNumber < 1024 || portNumber > 65535)
{
portNumber = portNumberErrorFix(0);
}
}
catch(Exception e)
{
while(portNumber == 0)
portNumber = portNumberErrorFix(1);
}
return portNumber;
}
private void runServer(int portNum) throws IOException
{
ServerSocket welcome = new ServerSocket(portNum);
while(true)
{
Socket clientConnection = welcome.accept();
HTTPRequest request = new HTTPRequest(clientConnection);
request.processRequest();
}
}
public void controller(String argument)
{
int portNumber = readArgs(argument);
try {
runServer(portNumber);
} catch (IOException e) {
System.out.println("ERROR!!!\nServer startup could not be performed");
System.exit(0);
}
}
public static void main(String[] args) {
WebServer test = new WebServer();
if(args.length > 0)
test.controller(args[0]);
else
{
System.out.println("No commands given in the command line, please try again");
System.exit(0);
}
}
}
my overall objective at this point is to be able to read a GET request line and headers made by my web-browser to my program(e.x. if i run my code and type "https://localhost:9876/index.html" into my chrome web browser I should read a request line and header information on my webserver. Now it is reading the request and printing with out error, but it is obviosuly in a unknown charset for my buffer
If you look at my imports you can see I've tried a multitude of solutions I have found on this site, but so far none have worked.
Edit 1: I have tried Roman idea to make sure the Eclipse console isnt the cause of this by changing the default charset on the console but got this result