September 04, 2010, 04:01:15 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Read data from a given url/domain name  (Read 442 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« on: June 10, 2009, 05:21:27 AM »

So i was browsing some of my old applications/demo classes and found this that i could share with you all. It reads a file from an URL and saves it in a file into your disk. Default file to read: http://engineeringserver.com/randomfile.txt

Output should be something like this:
Quote
HTTP/1.1 200 OK
Date: Wed, 10 Jun 2009 09:17:12 GMT
Server: Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 PHP/5.2.8
Last-Modified: Wed, 10 Jun 2009 09:17:06 GMT
ETag: "22eaccc-5a-46bfaf0652880"
Accept-Ranges: bytes
Content-Length: 90
Connection: close
Content-Type: text/plain

You just downloaded a textfile from http://www.engineeringserver.com / www.javaforums.net


Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. FileUpdater.java
  3.  ******************
  4. By: HappyFace http://www.engineeringserver.com
  5. Contact: info [@] engineeringserver.com
  6. Version: 8/August/2007
  7. Last updated: 10/June/2009
  8. "*****************
  9. Description:
  10. A demo how to read data from an url.
  11. Reference: http://en.wikipedia.org/wiki/List_of_HTTP_headers
  12. To do:
  13. -
  14. //----------------------------------------------------------------------*/
  15. package console;
  16.  
  17. import java.awt.Desktop;
  18. import java.io.BufferedWriter;
  19. import java.io.File;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.io.PrintStream;
  24. import java.net.Socket;
  25. import java.net.UnknownHostException;
  26. import java.util.Scanner;
  27.  
  28. public class HttpRequester {
  29. static Thread t = null;
  30. public static void main(String[] args) throws IOException {
  31. HttpRequester gr = new HttpRequester();
  32. gr.Update();
  33. }
  34.  
  35. public void Update(){
  36. String host = "www.engineeringserver.com";
  37. String saveLocation = "c:\\webPage.txt";
  38. Socket s;
  39.  
  40. try {
  41. s = new Socket(host, 80);
  42. PrintStream p = new PrintStream(s.getOutputStream());
  43. p.print("GET /randomfile.txt HTTP/1.0\r\n");
  44. p.print("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n");
  45. p.print("Host: " + host + "\r\n");
  46. p.print("Connection: close\r\n\r\n");
  47.  
  48. InputStreamReader in = new InputStreamReader(s.getInputStream());
  49. Scanner sc = new Scanner(in);
  50.  
  51. FileWriter fwe = new FileWriter(saveLocation);
  52.  
  53. int lines = 0;
  54. while (sc.hasNextLine()){
  55. String readData = sc.nextLine();
  56. System.out.println(lines + ") " + readData);
  57. bw.write(readData);
  58. bw.newLine();
  59. lines++;
  60. }
  61.  
  62. bw.close();
  63. fwe.close();
  64. Desktop.getDesktop().open(new File(saveLocation));
  65.  
  66. } catch (UnknownHostException e) {
  67. e.printStackTrace();
  68.  
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74.  
Created by GeSHI 1.0.7.20
« Last Edit: June 10, 2009, 06:03:33 AM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
« on: June 10, 2009, 05:21:27 AM »

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.227 seconds with 32 queries.

Google visited last this page Yesterday at 09:17:33 AM