3

What I need to do is send a username and password to a php script via a HTTP POST request so that I can query a database for the correct information. Currently I am stuck on both sending the POST request as well as receiving it.

To send a username and password I am using the following:

public class post {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost("http://www.example.com/practice.php");

        // Request parameters and other properties.
        List<NameValuePair> params = new ArrayList<NameValuePair>(2);
        params.add(new BasicNameValuePair("username", "user"));
        params.add(new BasicNameValuePair("password", "hunter2"));
        httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

        //Execute and get the response.
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream instream = entity.getContent();
            try {
                // do something useful
            } finally {
                instream.close();
            }
        }

    }
}

The php script I am using to collect the information is the following, it's simple but it's just for testing at the moment.

<?php
$username = $_POST['username'];
$password = $_POST['password'];
echo "username = $username<br>";
echo "password = $password<br>";
 ?>

I was wondering if someone could help me out by moving me in the correct direction of accepting a HTTP POST request in php from Java, or if I am even sending the post request correctly, any help is greatly appreciated.

terrabl
  • 743
  • 3
  • 8
  • 23

1 Answers1

3

Now, there are a few things, I would suggest you to keep in mind, while doing this.

  1. Try Making use of JSON: Json stands for JavaScript Object Notation. Itis a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write.

    public static void main(String[] args){
    JSONObject obj = new JSONObject();
    
    obj.put("username", username);
    obj.put("password", password);
    System.out.print(obj);
    // And then, send this via POST Method.
    }
    }
    

    For Php part,

    ...
    $data = file_get_contents("php://input");
    $json = json_decode($data);
    $username = $json['username'];
    $password = $json['password'];
    ...
    

Here is a good reference for that Json

  1. Make Use of Sessions When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn't maintain state. Session variables solve this problem by storing user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser.

    <?php
    $_SESSION["user"] = "green";
    echo "Session variables are set.";
    // Now store this in your database in a separate table and set its expiry date and time.
    ?>
    

    Here is a reference to that as well sessions.

  2. Use SSL : Secure Socket Layer (SSL) technology is security that is implemented at the transport layer.SSL allows web browsers and web servers to communicate over a secure connection. In this secure connection, the data that is being sent is encrypted before being sent and then is decrypted upon receipt and before processing. Both the browser and the server encrypt all traffic before sending any data. SSL addresses the following important security considerations.

a. Authentication: During your initial attempt to communicate with a web server over a secure connection, that server will present your web browser with a set of credentials in the form of a server certificate. The purpose of the certificate is to verify that the site is who and what it claims to be. In some cases, the server may request a certificate that the client is who and what it claims to be (which is known as client authentication).

b. Confidentiality: When data is being passed between the client and the server on a network, third parties can view and intercept this data. SSL responses are encrypted so that the data cannot be deciphered by the third party and the data remains confidential.

c. Integrity: When data is being passed between the client and the server on a network, third parties can view and intercept this data. SSL helps guarantee that the data will not be modified in transit by that third party.

And, Here are a few references for that as well. SSL Establishment Documentation, SSL with Java

Panda
  • 2,400
  • 3
  • 25
  • 35
  • Is sending the JSON similar to how I was sending the post request in the code above, or how would I POST that JSON to my php script. Also, what does $data = file_get_contents("php://input"); mean? @M.S.P – terrabl Mar 26 '16 at 17:08
  • that's the key to your answer. `php://input`. http://php.net/manual/en/wrappers.php.php – Panda Mar 26 '16 at 17:10
  • I just suggested another way of doing it, through Json. coz json is one of the primary way that an API uses, to communicate. So, if you are planning to deploy a service, JSON sounds the best, to many. You can also use XML for the same. – Panda Mar 26 '16 at 17:13
  • I don't really know where I am getting stuck, I am just having trouble with the whole server side stuff, like sending post requests and then having php on the server side accept them. – terrabl Mar 26 '16 at 17:17
  • I suggest you, to Post your approach in another question, specifically Highlighting what you want to do! And also specifying the error, if you are facing! For this question, Is their anything else, that's unclear? – Panda Mar 26 '16 at 17:21
  • Nope, I just accepted your answer, I'll make sure to post another one soonish. – terrabl Mar 26 '16 at 17:25