到Aruba托管的Java发布请求失败

时间:2018-11-03 15:40:06

标签: java php http post request

由于这个问题,我遇到了很大的麻烦。 我无法从Java客户端获取PHP API中的POST数据。 我仅在Aruba托管中遇到此问题,而在本地开发或其他托管站点中却没有

这是Java请求:

urlParameters = "name=Jack";
    byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);

    try {

        URL myurl = new URL(url);
        con = (HttpURLConnection) myurl.openConnection();

        con.setDoOutput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", "Java client");
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        con.setRequestProperty("Content-Length", String.valueOf(postData.length));
        try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
            wr.write(postData);
        }

        StringBuilder content;

        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()))) {

            String line;
            content = new StringBuilder();

            while ((line = in.readLine()) != null) {
                content.append(line);
                content.append(System.lineSeparator());
            }
        }

        System.out.println(content.toString());

    }catch(MalformedURLException e){
        System.out.println("Error "+e);
    }catch(IOException e){
        System.out.println("Error "+e);
      }
    finally {

        con.disconnect();
    }

在PHP中我也得到:

if($_SERVER['REQUEST_METHOD'] === 'POST')  false
$entityBody = file_get_contents('php://input');  empty string

我也尝试过更改php版本,但是什么也没有... 感谢您的帮助!

使用Wireshark,我明白了。我不是专家,但是我认为请求是通过代理重定向的,并变成GET

enter image description here

好吧,我找到了另一个线索 即使使用html表单,它也无法正常工作,但前提是我使用完整的网站路径

<form method="POST" action="http://example.com/php/api.php"> doesn't work
<form method="POST" action="/php/api.php"> work

现在我认为这是一个许可问题,但是我不知道什么。

1 个答案:

答案 0 :(得分:0)

使用以下格式的地址网址解决:“ www.exemple.com/page.php ”,而不是“ http://exemple.com/page.php ”或“ https://exemple.com/page.php ”。可能是由于提供商的DNS设置