【问题标题】:java http apache client does not receive the contentjava http apache客户端收不到内容
【发布时间】:2013-11-08 15:06:18
【问题描述】:

我正在使用用于 http 客户端的 Apache 客户端库将一些数据发布到我的服务器。

下面是代码,我得到了状态行响应,但我没有得到内容。 但是在wireshark上,我可以看到服务器响应的内容很少,例如内容类型、位置等,对于我的代码,我得到以下输出。

Status :: HTTP/1.1 201 Created Content null

请帮我看看我在阅读内容时哪里出错了,我需要一些代理相关的设置吗?

HttpClient client = new DefaultHttpClient();
String line = "";      String status = "";
HttpPost post = new HttpPost("http://127.0.0.1/msg");
try {

  HttpEntity e = new StringEntity("POSTING TO SERVER FOR TESTING");
  post.setEntity(e);


  HttpResponse response = client.execute(post);
  BufferedReader rd = new BufferedReader(new
  InputStreamReader(response.getEntity().getContent()));
  status = response.getStatusLine().toString() ;
  while ((line = rd.readLine()) != null) {
    System.out.println(line);
  }

} catch (IOException e) {
  e.printStackTrace();
}

System.out.println(" Status :: "+ status + " Content " + line);

【问题讨论】:

  • Status :: HTTP/1.1 201 Created Content null 的原因是因为分配给line 的最后一个值是null。响应可能没有任何内容。您可以根据需要使用 API 检查标头
  • “状态:”行之前打印了什么?那是你的内容。我怀疑这只是您的观察不佳,而不是真正的问题。

标签: java http apache-httpclient-4.x


【解决方案1】:

Status :: HTTP/1.1 201 Created Content null 的原因是因为分配给line 的最后一个值是null(这是您跳出循环的方式)。响应可能没有任何内容。您可以根据需要使用 API 检查标头

How to get headers? (java,httpclient 4.X) 为例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多