简体   繁体   English

如何从Socket获取查询字符串?

[英]How to get query string from Socket?

I'm coding web-server based on sockets. 我正在基于套接字编码Web服务器。 So I can get HTTP request headers: 因此,我可以获得HTTP请求标头:

import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

public class HttpServer {

    public static void main(String[] args) throws Throwable {
        //http://localhost:3000
        ServerSocket ss = new ServerSocket(3000);

        while (true) {
            //Waiting for socket
            Socket s = ss.accept();
            System.out.println("Client accepted");
            //The main process
            new SocketProcessor(s,ss).start();
        }
    }

    private static class SocketProcessor implements Runnable {
        private Thread t;
        private Socket s;
        private InputStream is;
        private OutputStream os;

        private SocketProcessor(Socket s,ServerSocket ss) throws Throwable {
            t = new Thread(this, "Server Thread");
            this.s = s;
            this.is = s.getInputStream();
            this.os = s.getOutputStream();
        }

        public void run() {
            try {
                readInputHeaders();
                writeResponse("<html><body><h1>Hello</h1></body></html>");


            } catch (Throwable t) {
                /*do nothing*/
            } finally {
                try {
                    s.close();
                } catch (Throwable t) {

                }
            }
            System.out.println("Client processing finished");
        }


        public void start()
        {
            t.start();
        }

        private void writeResponse(String s) throws Throwable {
            String response = "HTTP/1.1 200 OK\r\n" +
                    "Server: Server\r\n" +
                    "Content-Type: text/html\r\n" +
                    "Content-Length: " + s.length() + "\r\n" +
                    "Connection: close\r\n\r\n";
            String result = response + s;
            os.write(result.getBytes());
            os.flush();
        }

        private void readInputHeaders() throws Throwable {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while(true) {
                String s = br.readLine();
                System.out.println(s);
                if(s == null || s.trim().length() == 0) {
                    break;
                }
            }
        }
    }
}

Input: 输入:

http://localhost:3000/?page=1

Output: 输出:

GET /?page=1 HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,bg;q=0.2

But now I need to get query string params: 但是现在我需要获取查询字符串参数:

page=1

I know that GET-request is not the best example, because I can get params currently from URI, but this will not work with POST. 我知道GET-request不是最好的例子,因为我现在可以从URI中获取参数,但这不适用于POST。 So how can I get query string params from socket? 那么如何从套接字获取查询字符串参数呢? I have no idea to try. 我不知道尝试。

Of course I've found a solution. 我当然找到了解决方案。 POST parameters land after a new line. POST参数在新行之后登陆。 So checking if line is empty doesn't help. 因此,检查行是否为空无济于事。 We could check Content-Length and read request char by char. 我们可以检查Content-Length并逐个读取请求char。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM