首页 > 代码库 > HTTP 请求模拟

HTTP 请求模拟

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.InetSocketAddress;import java.net.Socket;public class HttpSimulator {    private Socket socket;    private int port = 80;    private String host = "localhost";    private String request = "";    private boolean isPost, isHead;            public void run() {                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));        while(true) {            try {                if(! readHostAndPort(reader)) {                    break;                }                readHttpRequest(reader);                sendHttpRequest();                readHttpResponse(reader);            }            catch (Exception e) {                System.out.println("err: " + e.getMessage());            }        }    }            public static void main(String[] args) {                new HttpSimulator().run();    }            private boolean readHostAndPort(BufferedReader consoleReader) throws IOException {                System.out.println("host:port> ");        String[] ss = null;        String s = consoleReader.readLine();        if(s.equals("q")) {            return false;                    }else{            ss = s.split("[:]");            if(!ss[0].equals(""))                 host = ss[0];            if(ss.length > 1)                 port = Integer.parseInt(ss[1]);            System.out.println(host + ":" + String.valueOf(port));            return true;        }    }        private void readHttpRequest(BufferedReader consoleReader) throws IOException {                System.out.println("input HTTP Request: ");        String s = consoleReader.readLine();        request = s + "\r\n";                isPost = s.substring(0, 4).equals("POST");        isHead = s.substring(0, 4).equals("HEAD");                while(!(s = consoleReader.readLine()).equals("")) {            request = request + s + "\r\n";        }                request = request + "\r\n";        if(isPost) {            System.out.println("input POST Method contents: ");            s = consoleReader.readLine();            request = request + s;        }    }            private void sendHttpRequest() throws IOException {                socket = new Socket();        socket.setSoTimeout(10 * 1000);        System.out.println("wait for connecting to server...");        socket.connect(new InetSocketAddress(host, port), 10 * 1000);        System.out.println("connected to server successfully!");                OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());        writer.write(request);        writer.flush();    }            private void readHttpResponse(BufferedReader consoleReader) {                String s = "";        try {            BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));            System.out.println("---------- HTTP Header ----------");            boolean b = true;            while((s = socketReader.readLine()) != null) {                if(s.equals("") && b == true && !isHead) {                    System.out.println("--------------------");                    b = false;                    System.out.println("Show HTTP contents or not (Y/N): ");                    String choice = consoleReader.readLine();                    if(choice.equals("Y") || choice.equals("y")) {                        System.out.println("---------- HTTP Contents ----------");                        continue;                    }else{                        break;                    }                }else{                    System.out.println(s);                }            }        }        catch (Exception e) {            System.out.println("err: " + e.getMessage());        }        finally {            try {                socket.close();            }            catch (Exception e) {                            }            System.out.println("--------------------");        }    }}

 

 

------ Result ------

host:port>
10.11.121.232:80
input HTTP Request:
GET / HTTP/1.0
Host:10.11.121.232

wait for connecting to server...
connected to server successfully!
---------- HTTP Header ----------
HTTP/1.1 200 OK
Date: Fri, 21 Nov 2014 07:45:18 GMT
Server: Apache/2.2.22 (Ubuntu)
Last-Modified: Wed, 12 Nov 2014 13:06:02 GMT
ETag: "20831-b1-507a90ecdbd88"
Accept-Ranges: bytes
Content-Length: 177
Vary: Accept-Encoding
Connection: close
Content-Type: text/html
--------------------
Show HTTP contents or not (Y/N):

 

HTTP 请求模拟