how do you programatically navigate web pages and return back desired page to user?

From: Flip (!no_spam!phenry_w_at_hotmail.com)
Date: 11/11/03

  • Next message: Joshua: "Re: how do you programatically navigate web pages and return back desired page to user?"
    Date: Tue, 11 Nov 2003 02:07:24 GMT
    
    

    I have a web page which I would like to place a simple button on. This
    button would then connect to some other website, enter some data, submit the
    page, navigate to the next page, enter some data, submit, navigate again,
    and return the resulting page to the user. I've tried this with the code
    below but am having no end of troubles with this. Can anyone help? I'm
    sure I'm not the first person to try this.

    Some of the troubles I've encountered:
    -the remote site appears to require the navigation between pages to setup
    the session I think
        -I could be off the mark on this one as I'm learning
    -the webpages I've read aren't good at handling .asps, I keep getting
    MalformedURLException: no protocol
    -I would think java would be able to help me out here, no?
    ------------------------------------------------
    package network;
    import java.io.*;
    import java.net.*;
    import java.util.*;

    public class PostTest {
       public PostTest() {
       }

       public static void main(String[] args) {
          try {
             String fileName;
             if( args.length > 0 ){
                fileName = args[0];
             } else {
                fileName = "PostTest.properties";
             }

             Properties properties = new Properties();
             FileInputStream in = new FileInputStream( fileName );
             properties.load( in );
             URL url = new URL( properties.getProperty( "URL" ) );
             String retWebPage = doPost( url, properties );
             System.out.println( retWebPage );

          }
          catch (Exception ex) {
             ex.printStackTrace();
          }
       }

       public static String doPost( URL url, Properties props ) throws
    IOException {
          URLConnection connection = url.openConnection();
          connection.setDoOutput( true );
          PrintWriter out = new PrintWriter( connection.getOutputStream() );

          Enumeration enum = props.keys();

          while( enum.hasMoreElements() ){
             String name = (String)enum.nextElement();
             String value = (String)props.getProperty( name );
             out.print( name );
             out.print( '=' );
             out.print( URLEncoder.encode( value ) );
             if( enum.hasMoreElements() ){
                out.print( '&' );
             }
          }
          out.close();

          BufferedReader in = null;
          StringBuffer response = new StringBuffer();
          try{
             in = new BufferedReader( new InputStreamReader(
    connection.getInputStream() ) );
          } catch( IOException ioe ){
             if( !(connection instanceof HttpURLConnection ) ) throw ioe;
             InputStream err = ((HttpURLConnection)connection).getErrorStream();
             if( err == null ) throw ioe;
             in = new BufferedReader( new InputStreamReader( err ) );
          }
          String line;
          while( ( line = in.readLine() ) != null ) response.append( line +
    "\n" );
          in.close();
          return response.toString();
       }
    }


  • Next message: Joshua: "Re: how do you programatically navigate web pages and return back desired page to user?"