无法使用从Java到PHP的POST请求发送数据

时间:2015-07-10 17:59:08

标签: java php networking

我正在尝试使用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:图片: wireshark img

1 个答案:

答案 0 :(得分:0)

问题是我用SHA-1加密了整个data = randomnumber事物。 data = part不应加密,需要稍后添加。