我正在写一个Java CGI客户端来与PHP-CGI交谈,但我碰到了一个凹凸,我的php $_POST
没有填充任何数据,而我肯定发送了一些。我不知道为什么这样做,我无法在任何地方找到这个问题。PHP-CGI post empty
我使用干净的PHP二进制文件直接从windows.php.net没有任何编辑。
这是我现在使用测试代码:
public static void main(String[] args)
{
HashMap<String, String> map = new HashMap<String, String>();
String body = "data=Foo+Bar";
String queryString = "yes=no&a=b";
String requestMethod = "POST";
String contentType = "application/x-www-form-urlencoded";
String contentLength = Integer.toString(body.length());
String docRoot = "D:/http test";
String scriptName = "/index.php";
String scriptFileName = docRoot + scriptName;
String pathInfo = "";
String pathTranslated = docRoot + pathInfo;
String requestUri = scriptName + pathInfo + ("?" + queryString);
String documentUri = scriptName + pathInfo;
String serverProtocol = "HTTP/1.1";
String gatewayInterface = "CGI/1.1";
map.put("QUERY_STRING" , queryString);
map.put("REQUEST_METHOD" , requestMethod);
map.put("CONTENT_TYPE" , contentType);
map.put("CONTENT_LENGTH" , contentLength);
map.put("SCRIPT_FILENAME" , scriptFileName);
map.put("SCRIPT_NAME" , scriptName);
map.put("PATH_INFO" , pathInfo);
map.put("PATH_TRANSLATED" , pathTranslated);
map.put("REQUEST_URI" , requestUri);
map.put("DOCUMENT_URI" , documentUri);
map.put("DOCUMENT_ROOT" , docRoot);
map.put("SERVER_NAME" , "localhost");
map.put("SERVER_PROTOCOL" , serverProtocol);
map.put("GATEWAY_INTERFACE" , gatewayInterface);
Client c = new Client("127.0.0.1" , 8090);
System.out.println("\n" + c.doRequest(map , body));
}
Client.java:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Map.Entry;
public class Client
{
private Socket socket;
public Client(String host, int port) throws UnknownHostException, IOException
{
socket = new Socket(host, port);
}
public String doRequest(Map<String, String> params, String content) throws IOException
{
ByteArrayOutputStream paramBytes = new ByteArrayOutputStream();
for (Entry<String, String> param: params.entrySet())
paramBytes.write(nvpair(param.getKey() , param.getValue()));
Packet beginRequest = new Packet(FCGI.BEGIN_REQUEST, FCGI.NULL_REQUEST_ID, new byte[] { 0, FCGI.RESPONDER, FCGI.KEEP_CONN, 0, 0, 0, 0, 0 });
Packet requestParams = new Packet(FCGI.PARAMS, FCGI.NULL_REQUEST_ID, paramBytes.toByteArray());
Packet requestContent = new Packet(FCGI.STDIN, FCGI.NULL_REQUEST_ID, content.getBytes("UTF-8"));
OutputStream stream = socket.getOutputStream();
stream.write(beginRequest.getBytes());
stream.write(requestParams.getBytes());
stream.write(requestContent.getBytes());
return readResponse();
}
private String readResponse() throws IOException
{
InputStream stream = socket.getInputStream();
// TODO buffering
String out = null;
for (Packet p = new Packet(stream); p.getType() != FCGI.END_REQUEST; p = new Packet(stream))
{
System.out.print(p.getType() + ", ");
if (p.getType() == FCGI.STDOUT)
out = new String(p.getContent());
}
return out;
}
public byte[] nvpair(String name, String value)
{
try
{
int nl = name.length();
int vl = value.length();
ByteArrayOutputStream bytes = new ByteArrayOutputStream(nl + vl + 10);
if (nl < 128)
bytes.write(b(nl));
else
bytes.write(new byte[] { b(nl >> 24), b(nl >> 16), b(nl >> 8), b(nl) });
if (vl < 128)
bytes.write(b(vl));
else
bytes.write(new byte[] { b(vl >> 24), b(vl >> 16), b(vl >> 8), b(vl) });
bytes.write(name.getBytes("UTF-8"));
bytes.write(value.getBytes("UTF-8"));
return bytes.toByteArray();
}
catch(IOException e)
{
e.printStackTrace();
}
return null;
}
public byte b(int i)
{
return (byte) i;
}
}
的index.php:
<pre><?php
echo "Hello World\n";
echo "REQUEST = "; print_r($_REQUEST);
echo "GET = "; print_r($_GET);
echo "POST = "; print_r($_POST);
echo php_ini_loaded_file(), "\n";
echo file_get_contents("php://input"), "\n";
echo php_sapi_name();
?></pre>
而且他的是结果我得到:
6,
X-Powered-By: PHP/5.6.3
Content-type: text/html; charset=UTF-8
<pre>Hello World
REQUEST = Array
(
[yes] => no
[a] => b
)
GET = Array
(
[yes] => no
[a] => b
)
POST = Array
(
)
D:\Programs\PHP 5.6.3 x64 TS\php.ini
cgi-fcgi</pre>
我想要的是$_POST
包含Array([data] => Foo Bar)
(这是我发送的)。
有谁知道我该如何修复它,以便$_POST
也填充数据?
哇,谢谢!今天我学到了一些新东西:)我会好好看看并做出改进。 – Gelunox 2014-12-13 15:27:14