首页 > 代码库 > 第一章 java网络编程入门_HTTPClient.java
第一章 java网络编程入门_HTTPClient.java
import java.net.*;
import java.io.*;
public class HTTPClient {
String host="www.baidu.com";
int port=80;
Socket socket;
public void createSocket()throws Exception{
socket=new Socket("www.baidu.com",80);
}
public void communicate()throws Exception{
System.out.println("---");
StringBuffer sb=new StringBuffer("GET "+"/"+" HTTP/1.1\r\n");
sb.append("Host: www.baidu.com\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; MSIE 6.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.println(new String(buffer.toByteArray())); //把字节数组转换为字符串
*/
InputStream socketIn=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(socketIn));
String data;
while((data=http://www.mamicode.com/br.readLine())!=null){
System.out.println(data);
}
socket.close();
}
public static void main(String args[])throws Exception{
HTTPClient client=new HTTPClient();
client.createSocket();
client.communicate();
}
}
import java.io.*;
public class HTTPClient {
String host="www.baidu.com";
int port=80;
Socket socket;
public void createSocket()throws Exception{
socket=new Socket("www.baidu.com",80);
}
public void communicate()throws Exception{
System.out.println("---");
StringBuffer sb=new StringBuffer("GET "+"/"+" HTTP/1.1\r\n");
sb.append("Host: www.baidu.com\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; MSIE 6.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.println(new String(buffer.toByteArray())); //把字节数组转换为字符串
*/
InputStream socketIn=socket.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(socketIn));
String data;
while((data=http://www.mamicode.com/br.readLine())!=null){
System.out.println(data);
}
socket.close();
}
public static void main(String args[])throws Exception{
HTTPClient client=new HTTPClient();
client.createSocket();
client.communicate();
}
}
第一章 java网络编程入门_HTTPClient.java
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。