socket模拟http请求

来源:互联网 发布:留学新加坡 知乎 编辑:程序博客网 时间:2024/05/21 07:55
    public static void main(String[] args) {        try {            Socket socket  = new Socket("220.181.111.86",80);            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));            printWriter.println("GET / HTTP/1.1");    //方式 根目录 协议            printWriter.println("Host:www.baidu.com");  //host            printWriter.println("Content-Type:text/html");            printWriter.println();            printWriter.flush();            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));            String str=null;           while ((str=bufferedReader.readLine())!=null){               System.out.println(str);           }            bufferedReader.close();            printWriter.close();            socket.close();        } catch (Exception e) {            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.        }    }
1.先在dos下ping www.baidu.com 拿到ip
2.注意
 printWriter.println(); printWriter.flush();
0 0