WebHttpClient.java
上传用户:cctqzzy
上传日期:2022-03-14
资源大小:12198k
文件大小:2k
- package chapter2;
- import java.io.*;
- import java.net.*;
- public class WebHttpClient {
- public static void main(String[] args) throws IOException {
- try {
- // 生成下载对象
- Socket webclient = new Socket("www.bnu.edu.cn", 80);
- PrintWriter result = new PrintWriter(webclient.getOutputStream(), true);
- BufferedReader receiver = new BufferedReader(new InputStreamReader(webclient.getInputStream()));
- //发送HTTP request请求
- result.println("GET / HTTP/1.1");
- result.println("Host: bnu.edu.cn");
- result.println("Connection: Close");
- result.println();
-
- //接收HTTP Response 返回的结果信息
- boolean bRet = true;
- StringBuffer sb = new StringBuffer(8096);
- while (bRet) {
- if (receiver.ready()) {
- int idx = 0;
- while (idx != -1) {
- idx = receiver.read();
- sb.append((char) idx);
- }
- bRet = false;
- }
- }
-
- // 显示获得的网页正文,打印到控制台
- System.out.println(sb.toString());
- webclient.close();
- } catch (UnknownHostException e) {
- System.err.println("无法访问指定主机.");
- System.exit(1);
- } catch (IOException e) {
- System.err.println("下载失败,请检查输入地址是否正确。");
- System.exit(1);
- }
- }
- }