Java:如何阅读简单的Web服务器get-request

时间:2016-10-08 20:27:48

标签: java sockets character-encoding webserver get-request

所以这是我的简单网络服务器模拟的代码

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);
        }
    }

}

此时我的总体目标是能够将我的网络浏览器生成的GET请求行和标题读取到我的程序中(例如,如果我运行我的代码并在我的chrome web中输入“https://localhost:9876/index.html”浏览器我应该在我的网络服务器上读取一个请求行和标题信息。现在它正在读取请求并打印出错误,但是对于我的缓冲区来说,这显然是在一个未知的字符集中

output

如果你查看我的导入,你可以看到我已经尝试了我在这个网站上找到的众多解决方案,但到目前为止还没有一个有效。

编辑1:我尝试过罗马的想法,通过改变控制台上的默认字符集来确保Eclipse控制台不是原因,但得到了这个结果enter image description here

0 个答案:

没有答案