Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.EOFException;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8.  
  9.  
  10. public class Http {
  11.    
  12.     public static void main(String[] args){
  13.         System.out.println(getFileContents("http://google.com/"));
  14.     }
  15.    
  16.     public static String getFileContents(String u){
  17.         String contents = "";
  18.         BufferedReader br = null;
  19.         try{
  20.             URL url = new URL(u);
  21.             URLConnection urlconn = (URLConnection) url.openConnection();
  22.             urlconn.setConnectTimeout(100);
  23.             InputStream is = urlconn.getInputStream();
  24.             br = new BufferedReader(new InputStreamReader(is));
  25.         }catch(Exception e){}
  26.         try{
  27.             boolean eof = false;
  28.             String s = br.readLine();
  29.             while(!eof) {
  30.             contents = contents + s;
  31.             try {
  32.               s = br.readLine();
  33.               if (s == null) {
  34.                 eof = true;
  35.                 br.close();
  36.               }
  37.             } catch (EOFException eo) {
  38.               eof = true;
  39.             } catch (IOException e) {
  40.               return "null";
  41.             }
  42.           }
  43.         } catch (IOException e) {
  44.           return "null";
  45.         }
  46.         return contents.trim();
  47.       }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement