Advertisement
gewur33

HttpGET.java

Mar 4th, 2015
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. /**
  2.  * Created by f00 on 04.03.15.
  3.  */
  4.  
  5. import java.net.*;
  6. import java.io.*;
  7. import java.util.*;
  8.  
  9. public class HttpGET {
  10.  
  11.     public static SimpleURL url;
  12.  
  13.     public void getIt() throws Exception {
  14.  
  15.         userInput();
  16.  
  17.         try {
  18.             Socket socket = new Socket(this.getUrl().getHost(), 80);
  19.  
  20.             System.out.println("Host IP Address: "+ socket.getInetAddress().toString());
  21.  
  22.             PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
  23.             out.println("GET "+ this.getUrl().getPath() +" HTTP/1.0");
  24.             out.println(); //leerzeile, RFC norm..
  25.             out.flush();
  26.  
  27.             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  28.  
  29.             String inputLine;
  30.             String[] result = new String[2];
  31.             StringBuilder res = new StringBuilder();
  32.  
  33.             while ((inputLine = in.readLine()) != null) {
  34.                 res.append(inputLine);
  35.                 res.append("\n");
  36.             }
  37.  
  38.             result = res.toString().split("<!");
  39.             in.close();
  40.             System.out.println("################################### REQUEST #####################################");
  41.             System.out.println(result[0]);
  42.             System.out.println("################################### CONTENT #####################################");
  43.             System.out.println(result[1]);  // result[0] contains the requestinformation
  44.  
  45.             socket.close();
  46.         } catch (Exception e) {
  47.             e.printStackTrace();
  48.         }
  49.     }
  50.  
  51.     public void userInput() {
  52.         System.out.println("Socket Browser v1.0");
  53.         System.out.println("Enter URL:");
  54.         Scanner scanner = new Scanner(System.in);
  55.  
  56.         SimpleURL newurl = new SimpleURL(scanner.nextLine());
  57.         this.setUrl(newurl);
  58.  
  59.         System.out.println("connecting to " + this.getUrl().getHost() + "....");
  60.  
  61.     }
  62.  
  63.     public  SimpleURL getUrl() {
  64.         return url;
  65.     }
  66.  
  67.     public void setUrl(SimpleURL url) {
  68.         HttpGET.url = url;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement