Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6. import java.util.Scanner;
  7.  
  8. //Để đổi tên hàng loạt: chuột phải tại tên biến muốn đổi => Refacter => Rename => Continue
  9. public class InternetJava {
  10.     //Dừng chương trình khi gặp lỗi IOException
  11.     public static void main(String[] args) throws IOException{
  12.         //Lần lượt là tên miền, tài nguyên, cổng
  13.         String host = "www.uow.edu.au";
  14.         String resource = "/student/index.html";
  15.         final int PORTHTML = 80;
  16.        
  17.         //Mở socket phía client
  18.         Socket socket = new Socket(host, PORTHTML);
  19.         //Lấy input stream và output stream
  20.         InputStream inputStream = socket.getInputStream();
  21.         OutputStream outputStream = socket.getOutputStream();
  22.        
  23.         //Wrap (bọc đối tượng) inputSream và outputSream
  24.         Scanner in = new Scanner(inputStream);
  25.         PrintWriter out = new PrintWriter(outputStream);
  26.        
  27.         out.println("GET " +resource +" HTTP/1.1\r\n" +
  28.                 "Host: " + host + "\r\n\n");
  29.        
  30.         //Xoá bộ đệm
  31.         out.flush();
  32.        
  33.         //Xử lý thông tin nhận về
  34.         //Chừng nào còn dòng tiếp theo
  35.         while(in.hasNextLine()) {
  36.             String input = in.nextLine();
  37.             System.out.println(input);
  38.         }
  39.        
  40.         socket.close();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement