使用socket发送http请求

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

import java.util.HashMap;

import java.util.Map;


public class LearnHttp {  

    private static final byte CR = '\r';  

    private static final byte LF = '\n';  

    private static final byte[] CRLF = {CR, LF};  

  

    public static void main(String[] args) throws UnknownHostException, IOException {  

        new LearnHttp().testHttp();  

    }  

      

    public void testHttp() throws UnknownHostException, IOException {  

        String host = "www.baidu.com";  

        Socket socket = new Socket(host, 80);  

          

        OutputStream out = socket.getOutputStream();  

        InputStream in = socket.getInputStream();  

          

        // 在同一个TCP连接里发送多个HTTP请求  

       

            writeRequest(out, host);  

            readResponse(in);  

            System.out.println("\n\n\n");  

       

    }  

      

    private void writeRequest(OutputStream out, String host) throws IOException {  

        // 请求行  

        out.write("GET /index.html HTTP/1.1".getBytes());  

        out.write(CRLF);        // 请求头的每一行都是以CRLF结尾的  

          

        // 请求头  

        out.write(("Host: " + host).getBytes()); // 此请求头必须  

        out.write(CRLF);  

  

        out.write(CRLF);        // 单独的一行CRLF表示请求头的结束  

  

        // 可选的请求体。GET方法没有请求体  

          

        out.flush();  

    }  

  

    private void readResponse(InputStream in) throws IOException {  

        // 读取状态行  

        String statusLine = readStatusLine(in);  

        System.out.println("statusLine :" + statusLine);  

          

        // 消息报头  

        Map<String, String> headers = readHeaders(in);  

          

        int contentLength = Integer.valueOf(headers.get("Content-Length"));  

          

        // 可选的响应正文  

        byte[] body = readResponseBody(in, contentLength);  

          

        String charset = headers.get("Content-Type");  

        if(charset.matches(".+;charset=.+")) {  

            charset = charset.split(";")[1].split("=")[1];  

        } else {  

            charset = "utf-8";     // 默认编码  

        }  

          

        System.out.println("content:\n" + new String(body, charset));  

    }  

      

    private byte[] readResponseBody(InputStream in, int contentLength) throws IOException {  

          

        ByteArrayOutputStream buff = new ByteArrayOutputStream(contentLength);  

          

        int b;  

        int count = 0;  

        while(count++ < contentLength) {  

            b = in.read();  

            buff.write(b);  

        }  

          

        return buff.toByteArray();  

    }  

      

    private Map<String, String> readHeaders(InputStream in) throws IOException {  

        Map<String, String> headers = new HashMap<String, String>();  

          

        String line;  

          

        while(!("".equals(line = readLine(in)))) {  

            String[] nv = line.split(": ");     // 头部字段的名值都是以(冒号+空格)分隔的  

            headers.put(nv[0], nv[1]);  

        }  

          

        return headers;  

    }  

      

    private String readStatusLine(InputStream in) throws IOException {  

        return readLine(in);  

    }  

      

    /** 

     * 读取以CRLF分隔的一行,返回结果不包含CRLF 

     */  

    private String readLine(InputStream in) throws IOException {  

        int b;  

          

        ByteArrayOutputStream buff = new ByteArrayOutputStream();  

        while((b = in.read()) != CR) {  

            buff.write(b);  

        }  

          

        in.read();      // 读取 LF  

          

        String line = buff.toString();  

          

        return line;  

    }  

    } 


转载于:https://my.oschina.net/u/876290/blog/372781

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值