使用Socket发送Http请求, 万能的Socket请求呀

直接上代码, 只有一个文件,就不上传源码了.

package com.zhai.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TestHttpClient {

    String host = "blog.foreverlove.us";
    String page = "/";
    int port = 80;
    Socket socket;

    public void createScoket() throws UnknownHostException, IOException {
        socket = new Socket(host, port);
    }

    public void comunicate() throws IOException {
        StringBuffer sb = new StringBuffer("GET " + page + " HTTP/1.1\r\n");
        sb.append("Host:" + host + "\r\n");
        sb.append("Content-Type: text/xml; charset=utf-8\r\n");
        sb.append("Accept:*/*\r\n");
        sb.append("Accept-Language:zh-cn\r\n");
        sb.append("Accept-Encoding:gzip,deflate:\r\n");
        sb.append("User-Agent:Mozilla/4.0(compatible;MEIE6.0;Windows NT 5.0)\r\n");
        sb.append("Connection:Keep-Alive\r\n\r\n");

        // 发出Http请求
        OutputStream socketOut = socket.getOutputStream();
        socketOut.write(sb.toString().getBytes());
        socket.shutdownOutput();

        // 接收响应的结果

       InputStream socketIn = socket.getInputStream();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        byte[] buff = new byte[1024];
        int len = -1;
        while ((len = socketIn.read(buff)) != -1) {
            buffer.write(buff, 0, len);

        }

        System.out.print(new String(buffer.toByteArray()));
        socket.close();

    }

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

        TestHttpClient client = new TestHttpClient();
        client.createScoket();
        client.comunicate();
    }

}

当数据量大的时候请使用以下的代码代替以上的红色部分代码

InputStream socketIn = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(socketIn));
        String data;
        while ((data = br.readLine()) != null) {
            System.out.print(data);
        }