#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    HELP with POST with URLCONNECTION


    Hello everyone i'm having some troubles completing this code so it clicks the "submit" button on the HTML page and returns the results in my java program.

    Here is HTML;
    .....

    Code:
    <p align="center">
    <input type="submit" name="Submit" value="Submit" onclick="return(ValideFormulaire())">
    </p>
    ......

    and here is my code;
    Code:
    import java.net.*;
    import java.io.*;
    
    public class URLConnectionWriter {
        public static void main(String[] args) {
            try {
                URL url;
                URLConnection urlConnection;
                DataOutputStream outStream;
                DataInputStream inStream;
         
                // Build request body
                String body =
                "Submit=" + URLEncoder.encode("Submit=", "iso-8859-1") +
                "submit=" + URLEncoder.encode("submit=", "iso-8859-1");
         
                // Create connection
                url = new URL("http://cdg5.intranet.bell.ca/BlackFoot/Ont/");
                urlConnection = url.openConnection();
                ((HttpURLConnection)urlConnection).setRequestMethod("POST");
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                urlConnection.setUseCaches(false);
                urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                urlConnection.setRequestProperty("Content-Length", ""+ body.length());
         
                // Create I/O streams
                outStream = new DataOutputStream(urlConnection.getOutputStream());
                inStream = new DataInputStream(urlConnection.getInputStream());
         
                // Send request
                outStream.writeBytes(body);
                outStream.flush();
                outStream.close();
         
                // Get Response
                // - For debugging purposes only!
                String buffer;
                while((buffer = inStream.readLine()) != null) {
                    System.out.println(buffer);
                }
         
                // Close I/O streams
                inStream.close();
                outStream.close();
            }
            catch(Exception ex) {
                System.out.println("Exception cought:\n"+ ex.toString());
            }
        }
            
        }
    If you could help it would be appreciated, thanks!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    bump
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    having some troubles completing this code
    Can you explain? Where is the button?
    What does the html code have to do with the java code?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Sorry I didn't really explain myself properly. So basically the button is located on another http url page, and the snippet of html code is from the website. I'm trying to fire post request and get a response after the button is clicked "submit".

    Does that make sense?
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Where is the java code used? What is the java code connected to the html?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    The java code is used in a compiler(eclipse).
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    What makes the java code start execution?
    What does that have to do with some user pressing a button on an html page?

    I don't see how the html page is connected or related to the java program.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    Ok so all I want it to the program to do is execute a post on a http page. Example, by executing the java file, it will type "dogs" into the search bar and click the submit in the html code.
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    want it to the program to do is execute a post on a http page.
    I don't understand how the java program is connected to an html page.

    executing the java file, it will type "dogs" into the search bar and click the submit in the html code.
    I do not see how executing the java program will do anything with some html code executing in a browser.

    Can you explain how the java program is connected to an html page being displayed in a browser.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    It's not connected, i want to see results inside the complier/program. There will be no html page open in a browser.
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    It seemed like you were talking about an html page. But now you are only interested in a java program that sends a POST request and gets a response from a server.

    What happens when you execute the program?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0
    It just gives me the html of the page I iniated it to, I want the html if the page after I send a POST. So I want the resulted pages after it sends a post command that selects "submit"
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    sorry, I don't know any way for a java program to do anything to change an html page that is loaded in a browser.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    77
    at http://ogameautomizer.com/ they have an automatic bot written in java and you can download their old version for free and view the code

    it does what your trying to do and more and its all in java
  28. #15
  29. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,553
    Rep Power
    1621

    Java - HTML directly without browser intervention?


    I am not sure what you are trying to accomplish even after reading your elaboration, but may be you shoud read about the following tutorial: Getting Started with Web Applications

    The java program of yours must be running as a server application which listens to a specific port for incoming requests. However, from the Java codes you posted, it seems like this is not the case. It is more like a web client application sending requests to some remote URL (hosted by a server). Hence, your entire program will not achieve what you desired.

    An application running at a server, listening to a port, can be hosted by an application server like WebLogic, WebSphere, Tomcat, Jetty etc. Alternatively, one may write a simple socket-based program listening to a port for any incoming requests sent by any clients.

    The web client must either be an application that is capable of firing HTTP requests (like chat programs) or a hosted webpage displayed by a web browser, which is capable of issuing HTTP requests upon form submission (ie clicking a Submit button). The browser will then fire the request to the URL, at which the server hosting this page is located.

    I think you have a wrong conceptual understanding of client-server application and multi-tiered architecture. It is strongly recommended that you read more introductory materials on Java client-server architecture and web application development related articles before revisiting this task.
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo