0

I want to send data from Java to a PHP page. I have googled a bit but the code I am finding is not working here is an example of the code I have used:

URL url = new URL("http://test.PHP");    
    URLConnection con = url.openConnection(); 
    con.setDoOutput(true);     
    PrintStream ps = new PrintStream(con.getOutputStream()); 
    ps.print("userid=12345");     
    ps.print("&password=Test");
    con.getInputStream();       
    ps.close(); 

What I need to do is input a username and password on the page and have the PHP page accept the input and actually run the log in function (Simulate as if a user pressed the button).

Thanks Ulrich

5
  • What do you mean input a username and password on the page, do you want to make POST request or what? Commented Jun 19, 2014 at 14:35
  • stackoverflow.com/questions/1359689/… - There is a good answer with how to send the query string as well. Commented Jun 19, 2014 at 14:37
  • and this answer on stackoverflow: stackoverflow.com/questions/2793150/… Commented Jun 19, 2014 at 14:39
  • Java & HTTP ? I have only one thing to say: Apache HttpClient Commented Jun 19, 2014 at 14:39
  • This actually has nothing to do with PHP; you're trying to programmatically log into a web page. The fact that it's backed by PHP doesn't change anything on your end. Commented Jun 19, 2014 at 16:06

1 Answer 1

2

When we try to use code to simulate action done in the browser , one of the best API in the Java world is HTTPClient, which give you many features that make your code act as a user in front of a browser (fill input, click, load link ...) . here is a good tutorial to start.

But in case that you want to use just the stardard Java API without any third party library, there here is a few lines that i hope it would be helpful :

first try to specify in the url the domaine name and the page that you want to invoke when requesting the server ex:

URL url = new URL("http://www.mydomaine.com/test.PHP");    

if the website show a popup for authentication login/pass when trying to access then try this url instead :

URL url = new URL("http://login:[email protected]/test.PHP");    

if it show you a login form and you try to access is by sending your login and pass as if you fill the input then, the first thing is to identify the name of the input that will be used when we send request to the server , it may be login and password or something else, to find those name try to inspect the page from the browser (view the source code of the page or try inspecting HTML elements) you will find something like this :

<form action="login.php" method="POST">
  <input type="text" name="my_login">
  <input type="password" name="my_password">
</form>

let's suppose that the names are my_login and my_password , in this case the the page that you have to invoke is login.php (which is the value of the action form) your code will be :

    URL obj = new URL("http://www.mudomaine.com/login.php);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST"); 
    String datas = "my_login=Administrator&my_password=s3cred0n3";
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(datas);
    wr.flush();
    wr.close();

You can handle the result by calling con.getInputStream() if you want to manage the server result .

But i will repeat again , if you can use a third library then use it , because it simplify the job , and reduce possible bug in your code.

Sign up to request clarification or add additional context in comments.

12 Comments

Hi, ok thanks a lot for the detailed response. I understand everything that you are saying and I think my code is doing the same thing the problem is that it is not actually logging in and changing from the "Login" page to the "Homepage" of the website but just logging in in the background and not changin the website page.
what do you want to do after login ?
I just want the "Homepage" to display. My app is opening and logging into a set of pre-selected programs. So all I need to do after logging in is displaying the next page which is the "Homepage".
So using your code I get the following error: java.lang.ClassCastException: sun.net.www.protocol.http.HttpURLConnection cannot be cast to javax.net.ssl.HttpsURLConnection
The website I am trying to access is the following: rccpdems01/TELEB/Login.php
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.