Facebook
From Idiotic Bushbaby, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 292
  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. public class HTTPWorker implements Runnable {
  9.  
  10.         private Socket socket;
  11.        
  12.         public HTTPWorker(Socket socket) {
  13.                 this.socket=socket;
  14.         }
  15.        
  16.         @Override
  17.         public void run() {
  18.                 try(InputStream is=socket.getInputStream();
  19.                                 OutputStream os=socket.getOutputStream();
  20.                                 Scanner sc=new Scanner(is);
  21.                                 PrintWriter pw=new PrintWriter(os,true)){
  22.                        
  23.                         while(sc.hasNextLine()) {
  24.                                 pw.println("Podaj nazwe serwera:");
  25.                                 String url=sc.nextLine();
  26.                                 pw.println("podaj numer portu:");
  27.                                 if(url!=null) {
  28.                                         String port=sc.nextLine();
  29.                                        
  30.                                         int portInt=Integer.parseInt(port);
  31.                                        
  32.                                         try(Socket s= new Socket(url,portInt);
  33.                                                         InputStream is2=s.getInputStream();
  34.                                                         OutputStream os2=s.getOutputStream();
  35.                                                         Scanner sc2=new Scanner(is2);
  36.                                                         PrintWriter pw2=new PrintWriter(os2)){
  37.                                                
  38.                                                 pw.println("GET /index.html");
  39.                                                 //pw.println("Host: google.com");
  40.                                                 //pw.println("Content- Type: text/html");
  41.                                                 pw.println("\n");
  42.                                                 pw.flush();
  43.                                                
  44.                                                 while(sc.hasNextLine()) {
  45.                                                         System.out.println(">"+sc.nextLine());
  46.                                                 }
  47.                                                
  48.                                         }
  49.  
  50.                                 }
  51.                         }
  52.                        
  53.                 } catch (IOException e) {
  54.                         // TODO Auto-generated catch block
  55.                         e.printStackTrace();
  56.                 }
  57.  
  58.         }
  59.  
  60. }
  61.