package com.meiyoudao.web;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author User
* HTTP1.0版本每一次只能跟服务器握手一次.
* HTTP1.1版本每一次建立连接,可以往返获取数据一直到客户端告诉服务端.
* 我获取完了.你可以关闭连接了.
*/
public class SocketHttp {
/**
* @param args
*/
public static void main(String[] args) {
try {
//Socket连接到的DNS地址
Socket socket = new Socket("meiyoudao.iteye.com",80);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//发送HTTP协议访问的头内容
bw.write("GET / HTTP/1.1");
bw.newLine();
bw.write("HOST:meiyoudao.iteye.com:80");
bw.newLine();
bw.write("Context-Type: text/html");
bw.newLine();
bw.newLine();
bw.flush();
//获取Socket返回的数据流信息
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String str= null;
while((str = br.readLine()) != null){
System.out.println(str);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
HTTP协议 与 Socket 的使用
最新推荐文章于 2024-11-06 15:48:00 发布