简体   繁体   中英

Java send http request to php server

I am trying to send http request to our php server with parameters. These parameters can suppose to pass to a function called LoadClassTimetable on a php servier. I am trying to get the response(some values) from this funciton. However, the server returns me error 400.

This is the server I am trying to connect

URL url = new URL(" http://appsvtmp.test.com ");

These are the parameters I try to send

    Map<String,Object> requestPayload = new LinkedHashMap<>();
    requestPayload.put("end_day", "2016-12-31 23:59:59+11:00");
    requestPayload.put("start_day", "2016-12-11 00:00:00+11:00");
    requestPayload.put("type", 1);
    requestPayload.put("user_id", 3360);

These are the headers I try to add.

    conn.setRequestMethod("POST");

    conn.setRequestProperty("x-amz-target", "SchoolLearnMeter_19801212.LoadClassTimetable");
    conn.setRequestProperty("x-amz-content-sha256", "bbace604ca933f1f7726e1f6ec9af2cc32e8b4dcd13285bd079a465d6e2790ed");
    conn.setRequestProperty("User-Agent", USER_AGENT);
    conn.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
    conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
    conn.setRequestProperty("Content-Length", String.valueOf(dataEntryBytes.length));

Are there anything wrong? Or I am completely wrong?

Error code 400 means that the request was malformed. In other words, the data stream sent by the client to the server didn't follow the rules.

Generally Error Code 400 is thrown by validators, so, please check your data. Following should be done in such scenario.

  1. Double check the documents provided for the api (if any).
  2. Check the server side code (if accessible).
  3. Do some trial/error on data.
  4. If any of those does not work or feasible then you have to talk to their developers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM