Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-19-2007, 05:01 AM   PM User | #1
Fws
New Coder

 
Join Date: Sep 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Fws is an unknown quantity at this point
Question Java connect to website

I am developing an application that can connect to a specific web site and use a file (i.e. xml file) to read information from. This file can then be updated and saved back to the web site. The only problem is that I don't know how to do this

I know how to read an xml file with Java but I don't know how to first pull the file off the Internet or read it directly from the Internet. Then the other problem is how to update the file and save it back on the Internet.

Any suggestions? Thanks for your help!
Fws is offline   Reply With Quote
Old 11-19-2007, 02:13 PM   PM User | #2
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
Post

Since you state that you already know how to parse through the XML, I'm just going to go ahead and give you some tips on how you can get to the website and pull down the data. In any case, this is how I've done it for some projects of mine.

First off, you need to import two of the .net classes, specifically HttpURLConnection and URL.

You then, want to create a URL to either the xml file or the webpage you are going to. Then you can use the HttpURLConnection to open the URL, set the request method as GET (since you're getting information from the server), perhaps include browser specific information to make it convinced you're not a bot, and then connect. Here is a quick snippet of code that I have no tested:

Code:
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class getXML 
{
  public static void main(String args[])
  {
    try {
      URL ourURL = new URL("http://www.codingforums.com/external.php?type=RSS2"); //Coding Forums RSS Feed
      HttpURLConnection huc = (HttpURLConnection)url.openConnection();            
      huc.setRequestMethod("GET");
      huc.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; JVM)");                        
      huc.setRequestProperty("Pragma", "no-cache");
      huc.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(huc.getInputStream()));

      String line = null;
      while ((line = reader.readLine()) != null) {
          // Either do your parsing here, or append it to a StringBuffer for later use
      }
      catch(IOException ioe)
      {
          ioe.printStackTrace();
       }
      catch(Exception e)
      {
         System.err.println("General Exception " + e);
         e.printStackTrace();
       }
    }
  }
}
Obviously I didn't close anything, connection or streamwise and you would wand to fix that in your code.

I actually learned this bit of code by looking at someone else's so I don't feel too bad about sharing it around. But, for more information I suggest you look at these things here:

URL
HttpURLConnection
Probably the best source for URL stuff:

Sun Tutorial on URL's

NOTE: Interesting enough, the Sun tutorial comes up with a more smaller solution then what I came up with that might be easier to understand. So My suggestion is you read through that and use mine as a background.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch

Last edited by Aradon; 11-19-2007 at 02:15 PM.. Reason: Adding Note
Aradon is offline   Reply With Quote
Old 11-19-2007, 06:12 PM   PM User | #3
Fws
New Coder

 
Join Date: Sep 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Fws is an unknown quantity at this point
Hello Aradon,
Thanks your your reply! That was very helpful to get started with getting the information from the web site.

Do you have any tips for the second part? I would like to make changes to the xml file and then save over (re-upload) the current xml file on the Internet. Can this be done?
Fws is offline   Reply With Quote
Old 11-19-2007, 10:07 PM   PM User | #4
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
If you read through some more about the URL Tutorial on the sun site there is probably a shorter way to do this. However, the first solution to this problem that comes to mind is to recreate the xml file and upload it onto the webserver (assuming that this program is not running on the same server).

To do this you would probably want to use an already written FTP Library, to which there are several out there. My personal favorite for using FTP and not worrying about counting bits / second is to use the Apache Commons one, specifically the FTPClient Class.

You can find more information on that class, including implementation examples here:

Commons Net
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
Aradon is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:56 PM.


Advertisement
Log in to turn off these ads.