【问题标题】:Http 416, Requested range not satisfiableHttp 416,请求的范围不可满足
【发布时间】:2018-04-14 08:04:46
【问题描述】:

我正在使用http客户端获取数据:

public static String getHttpResponse(String url) {

//LOGGER.info("Download page context from URL " + url);
String httpClientResponse = null;
try {
  URI uri = new URIBuilder(url).build();
  HttpResponse response;

  HttpHost target = new HttpHost(uri.getHost());
  HttpGet request = new HttpGet(uri);

  //request.setConfig(config);
  request.addHeader(new BasicHeader("User-Agent", "Mozilla/5.0"));
  request.addHeader(new BasicHeader("Content-Type", "text/html"));
  request.addHeader("Accept-Ranges", "bytes=100-1500");

  org.apache.http.client.HttpClient
      client = HttpClients.custom().build();

  response = client.execute(target, request);

  //LOGGER.info("Status Line for URL {} is {}", uri.getHost() + File.separator + uri.getPath(), response.getStatusLine());

  InputStream inputStream = response.getEntity().getContent();

  if (inputStream == null || response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
    /*LOGGER.error("Non-success response while downloading image. Response {}", response.getStatusLine());
    LOGGER.error("Error while download data from url {}", url);*/
  } else {
    httpClientResponse = IOUtils.toString(inputStream, CharEncoding.UTF_8);
  }

} catch (Exception e) {
  System.out.println("Error while download content from URL");
}
return httpClientResponse;
}

另外:我们可以使用 Jsoup 做到这一点吗?

谢谢。

【问题讨论】:

  • 好的,但是我应该给出什么范围的字节呢?

标签: java jsoup httpclient


【解决方案1】:

替换:

request.addHeader("Accept-Ranges", "bytes=100-1500");

与:

request.addHeader("Range", "bytes=100-1500");

Accept-Ranges 标头是服务器响应的一部分,表示服务器接受部分请求。

在您的请求中,您应该使用Range 标头,它指示应该返回文档服务器的哪一部分。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Ranges https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range

【讨论】:

    猜你喜欢
    • 2011-08-18
    • 2017-07-28
    • 2017-07-26
    • 2014-06-03
    • 2017-08-09
    • 1970-01-01
    • 2021-08-07
    • 2021-01-04
    • 2015-02-04
    相关资源
    最近更新 更多