A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 彭小康 中级黑马   /  2012-12-14 11:34  /  1356 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 彭小康 于 2012-12-14 11:37 编辑

代码如下
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;
public class HttpConnection
{
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException
{
String ip = “127.0.0.1″ ;
int port = 80 ;
Socket soc = new Socket(ip, port);
System.out.println(“———————————-”);
System.out.println(“\n\n\n\n”);
BufferedReader in = new BufferedReader( new InputStreamReader(soc.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream()));
StringBuffer meth = new StringBuffer();
meth.append(“GET / HTTP/1.1\r\n”);
// meth.append(“accept:*/*\r\n”);
meth.append(“host:127.0.0.1\r\n”);
meth.append(“”);
meth.append(“”);
out.write(meth.toString());
out.write(“\r\n”);
out.flush();
String line = null ;
while( (line = in.readLine())!= null)
{
System.out.println(line);
}
}
}
输出
———————————-
HTTP/1.1 200 OK
Date: Tue, 06 Mar 2012 12:32:58 GMT
Server: Apache/2.2.22 (Win32)
Last-Modified: Tue, 06 Mar 2012 11:46:06 GMT
ETag: “b000000008d9e-57-4ba9196947acd”
Accept-Ranges: bytes
Content-Length: 87
Content-Type: text/html


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马