要看一下Html 请求头和响应头的格式
请求头需要两个换行
后面的每一行需要一个换行
package net.linxingyang.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestHttp {
public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(InetAddress.getByName("www.baidu.com"), 80));
OutputStream os = socket.getOutputStream();
os.write("GET / HTTP/1.1\n\n".getBytes());
// os.write("other header \n".getBytes());
InputStream is = socket.getInputStream();
int length = -1;
byte[] bytes = new byte[1024];
while (-1 != (length = is.read(bytes))) {
if (0 != length) {
System.out.print(new String(bytes, 0, length));
}
}
}
}