ridjis

Reading InputStream

Jan 27th, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. // In case of HTTP 403
  2. System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");
  3.  
  4. URL url = new URL("url");
  5. InputStream stream = url.openStream();
  6. BufferedReader br = new BufferedReader(new InputStreamReader(stream));
  7.  
  8. StringBuilder sb = new StringBuilder();
  9. String line;
  10.  
  11. while ((line = br.readLine()) != null) {
  12.     sb.append(line);
  13. }
  14. stream.close();
  15.  
  16. OR
  17.  
  18. URL url = new URL("url");
  19. InputStream stream = url.openStream();
  20. BufferedInputStream bis = new BufferedInputStream(stream);
  21.  
  22. StringBuilder sb = new StringBuilder();
  23. String line;
  24.  
  25. while (true) {
  26.     int data = bis.read();
  27.     if (data == -1){
  28.         break;
  29.     } else {
  30.         sb.append((char) data);
  31.     }
  32. }
  33. bis.close();
  34. stream.close();
Add Comment
Please, Sign In to add comment