【发布时间】:2015-07-10 17:59:08
【问题描述】:
我正在尝试使用 POST 请求将数据从 Java 代码发送到 PHP 代码。出于某种原因,在 PHP 方面我什么也没得到,但我的代码“有效”(没有异常发生)。可能是什么问题?提前致谢! 我的数据如下所示:
"data=" + sb.toString()
代码可以在下面找到:
public static void sendRequest(String encryptedString) {
HttpURLConnection connection = null;
BufferedReader br = null;
DataOutputStream dos = null;
try {
connection = (HttpURLConnection) new URL("http://localhost/something/function").openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setFixedLengthStreamingMode(encryptedString.length());
dos = new DataOutputStream(connection.getOutputStream());
dos.writeBytes(encryptedString);
dos.flush();
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Can't create connection...");
e.printStackTrace();
} finally {
try {
if(dos != null) dos.close();
if(connection != null) connection.disconnect();
if(br != null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
PS:图片:
【问题讨论】:
-
var_dump() PHP 函数返回以下内容:
null
-
您不是将其发送到“PHP”,而是发送到 Web 服务器(可能是 Apache),因此您应该检查 HTTP POST 是否到达 Apache。检查其访问日志以验证这一点。
-
我知道我正在将它发送到网络服务器,但我只想说我正在尝试将数据从 Java 代码传递到 PHP 代码。不过,我会尽快检查日志(如果我能在这个该死的 wamp 结构 xD 中找到它们),谢谢您的建议
-
我检查了日志,它说 POST 通过(200 OK)。我认为问题一定出在PHP方面。或者可能由于某种原因,我的 Java 代码不会将必要的数据写入输出流。
-
尝试使用类似 google.com 的方式更改 url,如果它可以工作,那么它来自 PHP
标签: java php networking