dongyimeng3764 2018-09-06 04:36
浏览 73

接收GET请求而不是POST

I'm trying to create a client server application and I'm currently stuck. I have this java code on my client.

        HttpURLConnection connection;
        connection = (HttpURLConnection) new URL("http://www.masterpaint.gr/login.php").openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
        connection.setRequestProperty("Accept","application/json: charset=UTF-8");
        connection.setRequestMethod("POST");

        System.out.println("The request method on client end is " + connection.getRequestMethod());
        System.out.println("Server response to connection " + connection.getResponseMessage());

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String buffer;
        StringBuilder stringBuilder = new StringBuilder();
        while ((buffer = reader.readLine()) != null) {
            stringBuilder.append(buffer);
        }
        System.out.println("The request methond on server end is " + stringBuilder.toString()); 

And this is the simple test php code on the server.

  <?php   echo $_SERVER['REQUEST_METHOD'];  ?>

Whenever I run this test java program and try to connect I get the same output.

The request method on client end is POST
Server response to connection OK
The request methond on server end is GET

The php script always echoes back that I'm sending a GET request even though my java code states that use a POST. I have tried connecting to the script through postman using different request methods and it's all fine, so the problem must be in the Java code. Any insight would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanfazhun0383 2018-09-06 04:57
    关注

    I am not having any experience of php but it seems like it is looking for content-length header which will not be send in your case so it is treating this as GET request.

    HttpURLConnection connection;
             connection = (HttpURLConnection) new URL("http://www.masterpaint.gr/login.php").openConnection();
             connection.setDoOutput(true);
             connection.setDoInput(true);
             connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
             connection.setRequestProperty("Accept","application/json: charset=UTF-8");
             connection.setRequestMethod("POST");
             connection.setFixedLengthStreamingMode(0);
             System.out.println("The request method on client end is " + connection.getRequestMethod());
             System.out.println("Server response to connection " + connection.getResponseMessage());
    
             BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
             String buffer;
             StringBuilder stringBuilder = new StringBuilder();
             while ((buffer = reader.readLine()) != null) {
                 stringBuilder.append(buffer);
             }
             System.out.println("The request methond on server end is " + stringBuilder.toString()); 
    

    This will set content length 0 as you are not sending any content.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看