Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HTTP get Request  RSS feed

 
Pauldon
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone got any sample Java code for HTTP get request. I want to get a file of the internet like an xml file and download it.How can you specify when u get the file where it will be stored. Cheers.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bookmark JavaAlmanac.com. Has all the examples you could want. For example, this one. If you need an overview of IO, consult this chapter in the Java Tutorial .
 
Pauldon
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code i have used as a get request to get an xml file and store it locally. The one problem i have is when its stored locally it doesn't appear in the format it should be, like an xml file structure its all over the place. Is there anyway i can sort this.

 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we read the API doc for java.io.BufferedReader, we see:

public String readLine() throws IOException

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

Does that answer your question?
[ April 21, 2006: Message edited by: Joe Ess ]
 
Pauldon
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers your a star.
 
Pauldon
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the file has been created locally. There is header information which has been placed in the file by the server due to the get request. Can i delete this information in java. It's just when i try to parse the xml file, i can't and i think it's the header information causing the problem.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use a plain socket. Use a URL like in the JavaAlmanac example I linked before. The URL class (actually a HttpURLConnection instance under the covers) takes care of all the housekeeping like headers and all you get back is the content.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!