简体   繁体   English

Java向php服务器发送http请求

[英]Java send http request to php server

I am trying to send http request to our php server with parameters. 我正在尝试使用参数向我们的php服务器发送http请求。 These parameters can suppose to pass to a function called LoadClassTimetable on a php servier. 这些参数可以假设传递给php servier上名为LoadClassTimetable的函数。 I am trying to get the response(some values) from this funciton. 我试图从这个函数得到响应(一些值)。 However, the server returns me error 400. 但是,服务器返回错误400。

This is the server I am trying to connect 这是我尝试连接的服务器

URL url = new URL(" http://appsvtmp.test.com "); URL 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. 错误代码400表示请求格式错误。 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. 通常验证器会抛出错误代码400,因此,请检查您的数据。 Following should be done in such scenario. 应该在这种情况下进行以下操作。

  1. Double check the documents provided for the api (if any). 仔细检查为api提供的文件(如果有的话)。
  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. 如果其中任何一个不起作用或不可行,那么你必须与他们的开发人员交谈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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