【问题标题】:Java send http request to php serverJava向php服务器发送http请求
【发布时间】:2016-12-22 04:13:01
【问题描述】:

我正在尝试使用参数向我们的 php 服务器发送 http 请求。这些参数可以假设传递给 php 服务器上名为 LoadClassTimetable 的函数。我正在尝试从此功能中获取响应(某些值)。但是,服务器返回错误 400。

这是我要连接的服务器

网址 url = 新网址("http://appsvtmp.test.com");

这些是我尝试发送的参数

    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);

这些是我尝试添加的标题。

    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));

有什么问题吗?还是我完全错了?

【问题讨论】:

  • 能给个完整的代码吗?

标签: java httprequest


【解决方案1】:

错误代码 400 表示请求格式错误。也就是说,客户端向服务器发送的数据流不符合规则。

验证器通常会抛出错误代码 400,因此,请检查您的数据。在这种情况下应进行以下操作。

  1. 仔细检查为 api 提供的文档(如果有)。
  2. 检查服务器端代码(如果可访问)。
  3. 对数据进行一些试验/错误。
  4. 如果其中任何一个不起作用或不可行,那么您必须与他们的开发人员交谈。

【讨论】:

  • 这是否意味着我尝试发送到服务器的参数或标头有问题?
猜你喜欢
  • 2012-12-20
  • 2010-11-13
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多