2

Main クラスでメソッド from (PHPMediator を拡張) を使用して POST リクエストlogin=root&password=rootを送信しています。authenthication.phpAuthorize(String login, String password)AuthMediator

問題は、私の PHP スクリプトが何も受信しないことです ( $_SERVER['QUERY_STRING'] は空ですが、GET を手動で実行しようとすると、そうではありません)。すべてのコードが1つのクラス(メインクラス)ファイルにある場合、機能しました(つまり、PHPがリクエストを取得して処理していました)。しかし、別のファイルに分割した後、動作しなくなりました。

面白いトリックをしているアクセス修飾子がいくつかあるのではないでしょうか? 私は Java を初めて使用しますが、Java がどのような副作用をもたらす可能性があるのか​​、まだ完全には理解していません。

ここに私の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;
    }
}

そして、それは親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();
    }

}
4

2 に答える 2

1

さて、私は間違いを見つけました。

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

メッセージをサーバーに送信した後、初期化する必要があります。

于 2013-03-10T19:00:49.607 に答える
1

POST 要求はクエリ文字列を生成しません。

于 2013-03-06T12:00:31.853 に答える