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

  • What do you mean `input a username and password on the page`, do you want to make POST request or what? – Unlink Jun 19 '14 at 14:35
  • http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java - There is a good answer with how to send the query string as well. – juanvan Jun 19 '14 at 14:37
  • and this answer on stackoverflow: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests/2793153#2793153 – JEY Jun 19 '14 at 14:39
  • Java & HTTP ? I have only one thing to say: [Apache HttpClient](https://hc.apache.org/httpclient-3.x/) – NiziL Jun 19 '14 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. – David Ehrmann Jun 19 '14 at 16:06

1 Answers1

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:password@www.mydomaine.com/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.

Mifmif
  • 2,887
  • 14
  • 21
  • 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. – user3742362 Jun 19 '14 at 15:21
  • what do you want to do after login ? – Mifmif Jun 19 '14 at 15:26
  • 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". – user3742362 Jun 19 '14 at 15:28
  • 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 – user3742362 Jun 19 '14 at 15:31
  • The website I am trying to access is the following: http://rccpdems01/TELEB/Login.php – user3742362 Jun 19 '14 at 15:33
  • http://stackoverflow.com/questions/16150089/how-to-handle-cookies-in-httpurlconnection-using-cookiemanager – Mifmif Jun 19 '14 at 15:34
  • Use HttpURLConnection instead of HttpsURLConnection – Mifmif Jun 19 '14 at 15:35
  • Ok that fixed it so here is the code I am currently using and it runs but nothing happens on the page: URL obj = new URL("http://rccpdems01/TELEB/Login.php"); HttpURLConnection con = (HttpURLConnection ) obj.openConnection(); con.setRequestMethod("POST"); String datas = "userid=F4737326&psword=test"; con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(datas); wr.flush(); wr.close(); – user3742362 Jun 19 '14 at 15:39
  • I did look at the link that you sent but I did not understand it's relation – user3742362 Jun 19 '14 at 15:39
  • use it to handle the cookie that come from the server , and use it's value in the header of your next request when you try to load HomePage . – Mifmif Jun 19 '14 at 15:46
  • Ok so I have been playing around with it and understand what I have to do the problem is when I am using the code you provided in your answer the result I am getting is exact same log in form's code without a error message or anything. Should I not maybe be setting the value of the button as well ? How would I do this ? Here is the buttons values - – user3742362 Jun 20 '14 at 12:23
  • @user3742362 you don't need it , did you try to look for what are the headers that come after submitting your request ? it could have a redirection header to the HomePage. – Mifmif Jun 20 '14 at 21:21