• 简易JAVA_WEB服务器处理POST请求得到内容


    package com.zhao.test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.URLDecoder;
    
    /**
     * 
     * @author
     * 
     */
    public class Sample_IIS {
        public static void main(String[] args) {
            startServer(9000);
        }
    
        private static void startServer(int port) {
            System.out.println("======>startServer");
            try {
                ServerSocket server = new ServerSocket(port);
                server.setSoTimeout(0);
                Socket socket = null;
                while ((socket = server.accept()) != null) {
                    socket.setSoTimeout(2000);
                    byte[] buffer = new byte[2048];
                    InputStream is = socket.getInputStream();
                    int len = is.read(buffer);
                    getContent(buffer, len);
                    responseText(socket,
                            "{"cmd":"response","inof":"success"}");
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("=====>");
        }
    
        /**
         * 
         * @param pSocket
         * @param content
         */
        private static void responseText(Socket pSocket, String content) {
            StringBuffer buffer = new StringBuffer();
            // buffer.append("HTTP/1.1 200 OK
    ");
            // buffer.append("Date: Tue, 14 Sep 1999 02:19:57 GMT
    ");
            // buffer.append("Server: Apache/1.2.6
    ");
            // buffer.append("Connection: close
    ");
            // buffer.append("Content-Type: text/html
    ");
            // buffer.append("
    ");
            buffer.append(content);
            try {
                OutputStream os = pSocket.getOutputStream();
                os.write(buffer.toString().getBytes());
                os.flush();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 得到内容
         * 
         * @param data
         * @param len
         * @return
         */
        private static String getContent(byte[] data, int len) {
            String result = "";
            String content = new String(data, 0, len);
            System.out.println("content: " + content);
            String[] lines = content.split("
    ");
            String firstStr = lines[0];
            // 判断是POST还是GET
            String method = firstStr.split(" ")[0].toLowerCase();
            System.out.println("method: " + method);
            if ("get".equals(method)) {
                // get
            } else {
                // post
                String line = lines[lines.length - 1];
                int index = line.indexOf("=");
                result = line.substring(index + 1);
                try {
    
                    result = URLDecoder.decode(result, "gbk");
                    System.out.println("result: " + result);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
    }
  • 相关阅读:
    【机器学习】算法原理详细推导与实现(一):线性回归
    《0~3岁孩子的 正面管教》——备忘
    马歇尔·卢森堡《非暴力沟通》——备忘
    李笑来《财富自由之路》——备忘
    select、poll、epoll之间的区别总结[整理]
    堆和栈区别
    Linux 文件系统剖析
    Inside The C++ Object Model(五)
    Inside The C++ Object Model(四)
    Inside The C++ Object Model(三)
  • 原文地址:https://www.cnblogs.com/afluy/p/3917106.html
Copyright © 2020-2023  润新知