I'm having a weird problem that i couldn't find a solution for. I revisited an old code that worked in the past perfectly but now somehow it doesn't work properly.
The project is an android application connected with a database. I have a login activity where a post request going to be executed to check if the user exists in the database. The problem is somehow the parameters sent with the post request are not really sent.
Putting the android code apart i tried to test the post request separately. I'm using wampserver 3.1.4
with PHP 5.6.3
, MySQL 5.7
and Apache 2.4
. To retrieve the parameters from the post request i'm using this code snippet:
$username = null;
$password = null;
var_dump($_POST);
if (isset($_POST['PARAM_EMAIL']))
$username = $mysqli->real_escape_string($_POST['PARAM_EMAIL']);
if (isset($_POST['PARAM_PASSWORD']))
$password = $mysqli->real_escape_string($_POST['PARAM_PASSWORD']);
i used postman
to test the request and var_dump()
to check the content of the params.
Here an example of how i wrote the request and what result i got :
http://192.168.2.103/GestionDesCamions/[email protected]&PARAM_PASSWORD=xx
<pre class='xdebug-var-dump' dir='ltr'>
<small>C:\wamp64\www\GestionDesCamions\check.php:12:</small>
<b>array</b>
<i>(size=0)</i>
<i>
<font color='#888a85'>empty</font>
</i>
</pre>false
it says post request doesn't contain any params therefore i got in the end false because the user couldn't be found.
I used !empty() instead of isset() and i got the same result. I tested other requests, GET requests in particular and they work fine. Any idea how can i solve this problem ? I'm using windows 10 as an operating system.
EDIT :
here is the POST method i'm using in my android app :
public boolean SendToUrl(String strURL,
ArrayList<NameValuePair> nameValuePairs) {
// Creer un buffer
StringBuilder builder = new StringBuilder();
// Creer un client Http
HttpClient client = new DefaultHttpClient();
// Creer un obejet httppost pour utiliser la methode post
HttpPost httppost = new HttpPost(strURL);
try {
// UrlEncodedFormEntit() : An entity composed of a list of
// url-encoded pairs
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// recuperer la reponse
HttpResponse response = client.execute(httppost);
StatusLine statusLine = response.getStatusLine();
// recuperer le ack
int statusCode = statusLine.getStatusCode();
// si ack =200 connexion avec succee
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
// erreur du chargement
Toast.makeText(_mContext, "pas de connexion", Toast.LENGTH_LONG)
.show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Boolean.parseBoolean(builder.toString());
}
I tried to approach this problem differently, i changed my android code a little and tried to send post requests with the library volley instead of what i used before and it throwed this error : Unexpected response code 403 for "URL"
After referring to this topic i found a solution which was simply to modify this document httpd-vhosts.conf
in Apache as follows :
require local
to Require all granted
.
And now the POST requests are allowed and even the old code snippet for http requests works. Thank you everyone for the help.