用java socket向http服务器发送get命令

代码如下

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


Here is body!

终于看到了期待已久的HTTP/1.1 200 OK 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值