469,352 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

filling a web page form by java

hsn
237 100+
hello everyone.
i am trying to write a program which will open a url connection to a website and to its form and fill it up with dat and then submitting this form.
when i run the program i know the fields name and what they are called in the scrip. know that it doesn't matter which scriptting language the webpage is using. But, if you needed to know it is javascript.
my question is if someone could help me a little here. i am reading some books here and there what i need is some tips or advice from those who wrote a similer program.

regarding
hsn
Aug 25 '08 #1
4 19231
BigDaddyLH
1,216 Expert 1GB
It sounds like you are trying to programmatically POST. The Jakarta Commons HTTPClient can help.
Aug 25 '08 #2
hsn
237 100+
It sounds like you are trying to programmatically POST. The Jakarta Commons HTTPClient can help.
thanks i forgot to include that it will be done by using the POST command not by the GET command
regards
hsn
Aug 25 '08 #3
hsn
237 100+
someone please give me some hints or ideas.
i want my java code to act like any user when the user fill up a form on an html page.
i don't want it to use a POST or GET commands, i don't want it to pass the values through the URL.
I want my program to act like a user , i want it to open a form, fill it up, and then press the submit button.
if anyone has any ideas i would be really greatful/

Regards
hsn
Aug 31 '08 #4
chaarmann
785 Expert 512MB
someone please give me some hints or ideas.
...
i don't want it to use a POST or GET commands, i don't want it to pass the values through the URL.
I want my program to act like a user , i want it to open a form, fill it up, and then press the submit button.

Regards
hsn
Please note: If a user presses the submit-button then exactly that happens: the system sends a POST or GET request. (I know that here are also some other exotic ways and browsers, but I speak of the common case). So your question is like saying I want to make a fried egg, but I don't want to use a raw egg and I don't want to use a pan for that which would be the simplest way. So literally speaking, your only option then is to get a cook that does it for you.

How to get a cook:
Just use a Windows macro recorder (like Easy Macro Recorder or so) and start it. Then open your explorer, go to the page, fill out the values, press submit and then stop the recorder. It produces a file which you can then easily execute in your java program with Runtime.exec().

But if you want to fry your eggs yourself, which I would prefer, here is the source code of a program that sends a POST-request and gets the response page back (the HTML source code is contained in the return string). Then you can parse that string for any result you are interested in. It is quite old and uses a non-blocking HTTPClient.jar-file for the request, but you can also do it in a similar way with the java.net.URL class which are include in the newer java versions.

For example you are on google-site, enter "easy makro recorder" and press "submit". Then the browser send a request like that:
"http://www.google.com/search?q=easy+makro+recorder".

Now the java program that does it for you:
Expand|Select|Wrap|Line Numbers
  1.  import HTTPClient.HTTPConnection;
  2.  import HTTPClient.HTTPResponse;
  3.  import HTTPClient.CookieModule;
  4.  
  5. String urlWithValues = "search?q="+ URLEncoder.encode("easy makro recorder");
  6. String host = "64.233.167.99"; // = google.com. You can also use InetAddress.getByName("google.com") instead.
  7. String port = "80";
  8. HTTPConnection connection = new HTTPConnection(host, port);
  9. HTTPResponse response = connection.Post(urlWithValues);
  10.  
  11. InputStream input = response.getInputStream();
  12. BufferedReader dataInput = new BufferedReader(new InputStreamReader(input));
  13. String searchResults = "";
  14. while (true)
  15. {
  16.     String line = dataInput.readLine();
  17.     if (line == null)  break;// end of response
  18.     searchResults += line;
  19. }
  20.  
  21. System.out.println("google found following results:" + searchResults); 
Sep 2 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by sentinel | last post: by
1 post views Thread by mmac | last post: by
2 posts views Thread by Jishnu | last post: by
1 post views Thread by Adam Smith | last post: by
reply views Thread by Wim Verhavert | last post: by
3 posts views Thread by Bill | last post: by
reply views Thread by Tim_Mac | last post: by
1 post views Thread by anilbhu | last post: by
1 post views Thread by iamvani | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.