Java Code Examples for okhttp3.internal.http.HttpHeaders#hasBody()

The following examples show how to use okhttp3.internal.http.HttpHeaders#hasBody() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Http1Codec.java    From styT with Apache License 2.0 6 votes vote down vote up
private Source getTransferStream(Response response) throws IOException {
  if (!HttpHeaders.hasBody(response)) {
    return newFixedLengthSource(0);
  }

  if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) {
    return newChunkedSource(response.request().url());
  }

  long contentLength = HttpHeaders.contentLength(response);
  if (contentLength != -1) {
    return newFixedLengthSource(contentLength);
  }

  // Wrap the input stream from the connection (rather than just returning
  // "socketIn" directly here), so that we can control its use after the
  // reference escapes.
  return newUnknownLengthSource();
}
 
Example 2
Source File: Http1Codec.java    From AndroidProjects with MIT License 6 votes vote down vote up
private Source getTransferStream(Response response) throws IOException {
  if (!HttpHeaders.hasBody(response)) {
    return newFixedLengthSource(0);
  }

  if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) {
    return newChunkedSource(response.request().url());
  }

  long contentLength = HttpHeaders.contentLength(response);
  if (contentLength != -1) {
    return newFixedLengthSource(contentLength);
  }

  // Wrap the input stream from the connection (rather than just returning
  // "socketIn" directly here), so that we can control its use after the
  // reference escapes.
  return newUnknownLengthSource();
}
 
Example 3
Source File: OkHttpInterceptor.java    From pandora with Apache License 2.0 5 votes vote down vote up
private static byte[] responseBodyAsBytes(Response response) {
    ResponseBody responseBody = response.body();
    if (responseBody == null || !HttpHeaders.hasBody(response)) {
        return null;
    }
    try {
        return sourceToBytesInternal(response.peekBody(Long.MAX_VALUE).source());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 4
Source File: PcGetQueueCountCrawler.java    From MyBlog with Apache License 2.0 4 votes vote down vote up
public String crawler(String username, String token, String trainDate, String trainNo,
                      String stationTrainCode, String seatType, String fromStationTelecode,
                      String toStationTelecode, String leftTicket, String train_location) {
    FormBody formBody = new FormBody.Builder(Util.UTF_8)
            .add("train_date", trainDate)
            .add("train_no", trainNo)
            .add("stationTrainCode", stationTrainCode)
            .add("seatType", seatType)
            .add("fromStationTelecode", fromStationTelecode)
            .add("toStationTelecode", toStationTelecode)
            .addEncoded("leftTicket", leftTicket)
            .add("purpose_codes", "00")
            .add("train_location", train_location)
            .add("json_att", StringUtils.EMPTY)
            .add("REPEAT_SUBMIT_TOKEN", token)
            .build();

    Request request = new Request.Builder()
            .url(URL)
            .header("Origin", "https://kyfw.12306.cn")
            .header("Accept", "application/json, text/javascript, */*; q=0.01")
            .header("X-Requested-With", "XMLHttpRequest")
            .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36")
            .header("Accept-Language", "zh-CN,zh;q=0.9")
            .header("Referer", "https://kyfw.12306.cn/otn/confirmPassenger/initDc")
            // 加入Accept-Encoding,okhttp不会自动解gzip。不加的话请求头有Accept-Encoding: gzip
            // .header("Accept-Encoding", "gzip, deflate, br")
            // FormBody会设置请求头Content-Type: application/x-www-form-urlencoded
            // .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
            .post(formBody)
            .build();

    try (Response resp = getClient(username).newCall(request).execute()) {
        if (!resp.isSuccessful() || !HttpHeaders.hasBody(resp)) {
            return StringUtils.EMPTY;
        }
        return resp.body().string();
    } catch (IOException e) {
        log.error("error", e);
    }
    return StringUtils.EMPTY;
}