0

I am trying to connect to an api written in php from a java client.

For simplicity of the issue, I reduced the api to the following: (which simply returns the request given to the server)

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
define('DATA_PATH', realpath(dirname(__FILE__).'/data'));
$applications = array(
   'APP001' => '28e336ac6c9423d946ba02d19c6a2632', //randomly generated app key  for php client
   'APP002' => '38e336ac6c9423d946ba02d19c6a2632' // for java app
);
require_once 'models/TodoItem.php';
echo"request";
foreach ($_REQUEST as $result) {
 echo $result;
 echo "<br>";
} 
echo"end";
exit();

I am sending the request as follows: (string param is the string in the code snippet after this)

URL url;
HttpURLConnection connection = null;  
try {
url = new URL(APP_URI);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", 
     "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + 
         Integer.toString(param.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");  
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
   connection.getOutputStream ());
wr.writeBytes (param);
wr.flush ();
wr.close ();
//Get Response  
InputStream is = connection.getInputStream();
// read from input stream

The request string being passed is as follows: (a json object with 2 params, one of which is another json object)

{"app_id":"APP002","enc_request":"{\"username\":\"nikko\",\"action\":\"checkUser\",\"userpass\":\"test1234\",\"controller\":\"todo\"}"}

The reply is as follows, which consist only of the start and end tags I'm manually echoing and no content:

 requestend

Why am I not getting any content on the server side?

mangusbrother
  • 3,988
  • 11
  • 51
  • 103
  • Did you forget to ask your question perhaps or am I missing something? – PeeHaa Jun 15 '14 at 00:08
  • where is the `json_decode` in your php for the received object? – Ohgodwhy Jun 15 '14 at 00:08
  • @PeeHaa Yeah you missed that, he asked why "no content" – Yang Jun 15 '14 at 02:21
  • Yes PeeHaa the issue is that I am not getting any content. i'll edit to make it more clear. And @Ohgodwhy json_decode needs a string, so i would need to access an entry in the arry to pass to it, yet there are no entries at all. – mangusbrother Jun 15 '14 at 08:11

1 Answers1

0

I ended up using apache's httpclient api. By combining the answers from the following questions: Sending HTTP POST Request In Java and What's the recommended way to get the HTTP response as a String when using Apache's HTTP Client? I go the following solution.

Note: The app_id and enc_request which i was sending as part of json are now as part of a namedpair, which adheres to the array being expected on the server side. Hence, the param string is now only:

 {"username":"nikko","action":"checkUser","userpass":"test1234","controller":"todo"}

The code is as follows:

public static String excutePost(String[][] urlParameters) {
        try {
            String param = encode(urlParameters);
            HttpClient httpclient = HttpClients.createDefault();
            HttpPost httppost = new HttpPost(APP_URI);
            List<NameValuePair> params = new ArrayList<NameValuePair>(2);
            params.add(new BasicNameValuePair("app_id", APP_NAME));
            params.add(new BasicNameValuePair("enc_request", param));
            httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                String res = EntityUtils.toString(entity);
                return res;
            }
        } catch (IOException e) {
            return null;
        }

        return null;
    }
Community
  • 1
  • 1
mangusbrother
  • 3,988
  • 11
  • 51
  • 103