2

I'm sending POST request login=root&password=root to authenthication.php by using Authorize(String login, String password) method from AuthMediator(which extends PHPMediator) in my Main class.

The problem is - my PHP script never receives anything($_SERVER['QUERY_STRING'] is empty, but when I try to execute GET manually - it isn't). It worked(i.e. PHP was getting requests and processing them) when all the code was in one class(Main class) file. But after I've splitted it into different files, it stopped working.

Maybe there's some access modificators playing some funny tricks? I'm new to Java, and I'm still not fully understanding, which side effects they can have.

Here is my authentication.php:

<?php
if(isset($_POST['login']) && isset($_POST['password']) )
{
    $login=$_POST['login'];
    $password=$_POST['password'];

    $userfile_path="../users/".$login;

    if(!file_exists($userfile_path))
    {
        echo "ERR_USER_INVALID";
    }
    else
    {
        $hash=md5($login.$password);

        $userfile=fopen($userfile_path,"r");
        if($userfile!=FALSE)
        {
            if(fgets($userfile)==$hash)
            {
                echo "OK_USER_VALID";
            }
            else
            {
                echo "ERR_USER_INVALID";
            }
            fclose($userfile);
        }
        else
        {
            echo "ERR_OPENING_USER_FAILED";
        }
    }

}
else
{
    echo "ERR_AUTH_MALFORMED_POST_REQUEST(".$_SERVER['QUERY_STRING'] .")";
}

?>

AuthMediator.class:

package WebActivity;

import Constants.MagicConstants;

public class AuthMediator extends PHPMediator {

    public AuthMediator() throws Exception {
        super(MagicConstants.AUTH_URL);
    }

    public String Authorize(String login, String password) {
        String _data="login="+login+"&password="+password;
        try {
            this.sendData(_data);
            this.receiveData();
        } catch (Exception ex) {
            return "ERR_EXCEPTION";
        }

        return this._response;
    }
}

And it's parent PHPMediator.class:

package WebActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;



public class PHPMediator {

    private URL _phpUrl;
    private URLConnection _urlConn;
    private OutputStreamWriter _outWrite;
    private BufferedReader _bufRead;
    protected String _response;


    PHPMediator(String url) throws Exception {
            this._response="";
            //opens connection
            this._phpUrl=new URL(url);
            this._urlConn=_phpUrl.openConnection();
            this._urlConn.setDoOutput(true);  

            //initializes writer and reader
            this._outWrite=new OutputStreamWriter(_urlConn.getOutputStream());
            this._bufRead=new BufferedReader(new InputStreamReader(_urlConn.getInputStream()));   
    }

    protected void setUrl(String url) throws Exception{
            //opens connection
            this._phpUrl=new URL(url);
            this._urlConn=_phpUrl.openConnection();
            this._urlConn.setDoOutput(true);  

            //initializes writer and reader
            this._outWrite=new OutputStreamWriter(_urlConn.getOutputStream());
            this._bufRead=new BufferedReader(new InputStreamReader(_urlConn.getInputStream()));     
    }

    protected void sendData(String data) throws IOException {
        String _data;
        _data=URLEncoder.encode(data, "UTF-8");
        this._outWrite.write(_data);
        this._outWrite.flush();


    }

    protected void receiveData() throws IOException {
        this._response+=this._bufRead.readLine();
        this._outWrite.close();
        this._bufRead.close();
    }

}
  • You should modify your line String _data="login="+login+"&password="+password; As this means you are sending you infor using get Method. I dont know java but you can search and find how send data with post – Rohit Choudhary Mar 06 '13 at 11:57
  • Here you can find a well explained example of how to do post requests via java: http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily – jpee Mar 10 '13 at 19:25

2 Answers2

1

POST requests do not generate a query string.

karmafunk
  • 1,453
  • 12
  • 20
1

Well, I found the mistake.

this._bufRead=new BufferedReader(new InputStreamReader(_urlConn.getInputStream()));

has to be initialized after sending the message to server.