获取POST请求的参数

时间:2014-01-12 15:54:28

标签: java sockets http post

我正在使用此代码获取POST请求的所有标头,但我无法获取参数(消息正文)...

这是代码:

BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
            BufferedOutputStream out = new     BufferedOutputStream(clientSocket.getOutputStream()); 

            String host = null;
            String url = null;
            int i=0;
            String [] headers = new String [100];
            String buffer;
            boolean post = false;

            while ((buffer = in.readLine()) != null) {  
                headers[i]=buffer;
                i++;
                if(buffer.startsWith("GET"))
                {
                    String[] splitText = buffer.split(" ");
                    url = splitText[1];
                    post = false;
                    }
                if(buffer.startsWith("POST"))
                {
                    String[] splitText = buffer.split(" ");
                    url = splitText[1];
                    post = true;
                    }
                if(buffer.startsWith("Host:"))
                {
                    String[] splitText = buffer.split(" ");
                    host = splitText[1];
                    }

                if (buffer.isEmpty()) break;
                }

这是我执行POST时的结果示例:

POST http://www.elotrolado.net/ucp.php?mode=login HTTP/1.1
Host: www.elotrolado.net
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: http://www.elotrolado.net/
Cookie: phpbb3_eol_u=1; phpbb3_eol_k=; phpbb3_eol_sid=5166ddca49bbeb53d4a407fffecda64a; __utma=71363783.573183633.1389391106.1389491494.1389539722.9; __utmz=71363783.1389391106.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); cw=1; __utmb=71363783.2.10.1389539722; __utmc=71363783
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 77

我得到的最后一个标题是Content-Length,但身体在哪里?

感谢。

0 个答案:

没有答案