来自Java的POST请求无法访问PHP

时间:2013-03-06 11:51:33

标签: java php post request

我通过在我的Main类中使用来自login=root&password=root(扩展PHPMediator)的authenthication.php方法向Authorize(String login, String password)发送POST请求AuthMediator

问题是 - 我的PHP脚本从未收到任何内容($_SERVER['QUERY_STRING']为空,但当我尝试手动执行GET时 - 它不是)。当所有代码都在一个类(Main类)文件中时,它工作(即PHP正在获取请求并处理它们)。但是在我把它分成不同的文件之后,它就停止了工作。

也许有一些访问修改器在播放一些有趣的技巧?我是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();
    }

}

2 个答案:

答案 0 :(得分:1)

POST请求不会生成查询字符串。

答案 1 :(得分:1)

好吧,我发现了错误。

this._bufRead=new BufferedReader(new InputStreamReader(_urlConn.getInputStream()));
将消息发送到服务器后,必须初始化