Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Fws
    Fws is offline
    New Coder
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts

    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.
    Last edited by Aradon; 11-19-2007 at 03:15 PM. Reason: Adding Note
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    Fws
    Fws is offline
    New Coder
    Join Date
    Sep 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •