okhttp3.internal.http.HttpHeaders Java Examples
The following examples show how to use
okhttp3.internal.http.HttpHeaders.
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 AndroidProjects with MIT License | 6 votes |
private void readChunkSize() throws IOException { // Read the suffix of the previous chunk. if (bytesRemainingInChunk != NO_CHUNK_YET) { source.readUtf8LineStrict(); } try { bytesRemainingInChunk = source.readHexadecimalUnsignedLong(); String extensions = source.readUtf8LineStrict().trim(); if (bytesRemainingInChunk < 0 || (!extensions.isEmpty() && !extensions.startsWith(";"))) { throw new ProtocolException("expected chunk size and optional extensions but was \"" + bytesRemainingInChunk + extensions + "\""); } } catch (NumberFormatException e) { throw new ProtocolException(e.getMessage()); } if (bytesRemainingInChunk == 0L) { hasMoreChunks = false; HttpHeaders.receiveHeaders(client.cookieJar(), url, readHeaders()); endOfInput(true); } }
Example #2
Source File: Http1Codec.java From AndroidProjects with MIT License | 6 votes |
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: Http1Codec.java From styT with Apache License 2.0 | 6 votes |
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 #4
Source File: Http1Codec.java From styT with Apache License 2.0 | 6 votes |
private void readChunkSize() throws IOException { // Read the suffix of the previous chunk. if (bytesRemainingInChunk != NO_CHUNK_YET) { source.readUtf8LineStrict(); } try { bytesRemainingInChunk = source.readHexadecimalUnsignedLong(); String extensions = source.readUtf8LineStrict().trim(); if (bytesRemainingInChunk < 0 || (!extensions.isEmpty() && !extensions.startsWith(";"))) { throw new ProtocolException("expected chunk size and optional extensions but was \"" + bytesRemainingInChunk + extensions + "\""); } } catch (NumberFormatException e) { throw new ProtocolException(e.getMessage()); } if (bytesRemainingInChunk == 0L) { hasMoreChunks = false; HttpHeaders.receiveHeaders(client.cookieJar(), url, readHeaders()); endOfInput(true); } }
Example #5
Source File: Cache.java From styT with Apache License 2.0 | 5 votes |
CacheRequest put(Response response) { String requestMethod = response.request().method(); if (HttpMethod.invalidatesCache(response.request().method())) { try { remove(response.request()); } catch (IOException ignored) { // The cache cannot be written. } return null; } if (!requestMethod.equals("GET")) { // Don't cache non-GET responses. We're technically allowed to cache // HEAD requests and some POST requests, but the complexity of doing // so is high and the benefit is low. return null; } if (HttpHeaders.hasVaryAll(response)) { return null; } Entry entry = new Entry(response); DiskLruCache.Editor editor = null; try { editor = cache.edit(key(response.request().url())); if (editor == null) { return null; } entry.writeTo(editor); return new CacheRequestImpl(editor); } catch (IOException e) { abortQuietly(editor); return null; } }
Example #6
Source File: ResponseHeaderRecord.java From mobile-buy-sdk-android with MIT License | 5 votes |
ResponseHeaderRecord(Response response) { this.url = response.request().url().toString(); this.varyHeaders = HttpHeaders.varyHeaders(response); this.requestMethod = response.request().method(); this.protocol = response.protocol(); this.code = response.code(); this.message = response.message(); this.responseHeaders = response.headers(); this.handshake = response.handshake(); this.sentRequestMillis = response.sentRequestAtMillis(); this.receivedResponseMillis = response.receivedResponseAtMillis(); }
Example #7
Source File: CacheStrategy.java From AndroidProjects with MIT License | 5 votes |
public Factory(long nowMillis, Request request, Response cacheResponse) { this.nowMillis = nowMillis; this.request = request; this.cacheResponse = cacheResponse; if (cacheResponse != null) { this.sentRequestMillis = cacheResponse.sentRequestAtMillis(); this.receivedResponseMillis = cacheResponse.receivedResponseAtMillis(); Headers headers = cacheResponse.headers(); for (int i = 0, size = headers.size(); i < size; i++) { String fieldName = headers.name(i); String value = headers.value(i); if ("Date".equalsIgnoreCase(fieldName)) { servedDate = HttpDate.parse(value); servedDateString = value; } else if ("Expires".equalsIgnoreCase(fieldName)) { expires = HttpDate.parse(value); } else if ("Last-Modified".equalsIgnoreCase(fieldName)) { lastModified = HttpDate.parse(value); lastModifiedString = value; } else if ("ETag".equalsIgnoreCase(fieldName)) { etag = value; } else if ("Age".equalsIgnoreCase(fieldName)) { ageSeconds = HttpHeaders.parseSeconds(value, -1); } } } }
Example #8
Source File: Response.java From AndroidProjects with MIT License | 5 votes |
/** * Returns the authorization challenges appropriate for this response's code. If the response code * is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the response code is * 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. Otherwise this * returns an empty list of challenges. */ public List<Challenge> challenges() { String responseField; if (code == HTTP_UNAUTHORIZED) { responseField = "WWW-Authenticate"; } else if (code == HTTP_PROXY_AUTH) { responseField = "Proxy-Authenticate"; } else { return Collections.emptyList(); } return HttpHeaders.parseChallenges(headers(), responseField); }
Example #9
Source File: Cache.java From AndroidProjects with MIT License | 5 votes |
public Entry(Response response) { this.url = response.request().url().toString(); this.varyHeaders = HttpHeaders.varyHeaders(response); this.requestMethod = response.request().method(); this.protocol = response.protocol(); this.code = response.code(); this.message = response.message(); this.responseHeaders = response.headers(); this.handshake = response.handshake(); this.sentRequestMillis = response.sentRequestAtMillis(); this.receivedResponseMillis = response.receivedResponseAtMillis(); }
Example #10
Source File: Cache.java From AndroidProjects with MIT License | 5 votes |
CacheRequest put(Response response) { String requestMethod = response.request().method(); if (HttpMethod.invalidatesCache(response.request().method())) { try { remove(response.request()); } catch (IOException ignored) { // The cache cannot be written. } return null; } if (!requestMethod.equals("GET")) { // Don't cache non-GET responses. We're technically allowed to cache // HEAD requests and some POST requests, but the complexity of doing // so is high and the benefit is low. return null; } if (HttpHeaders.hasVaryAll(response)) { return null; } Entry entry = new Entry(response); DiskLruCache.Editor editor = null; try { editor = cache.edit(key(response.request().url())); if (editor == null) { return null; } entry.writeTo(editor); return new CacheRequestImpl(editor); } catch (IOException e) { abortQuietly(editor); return null; } }
Example #11
Source File: CacheStrategy.java From styT with Apache License 2.0 | 5 votes |
public Factory(long nowMillis, Request request, Response cacheResponse) { this.nowMillis = nowMillis; this.request = request; this.cacheResponse = cacheResponse; if (cacheResponse != null) { this.sentRequestMillis = cacheResponse.sentRequestAtMillis(); this.receivedResponseMillis = cacheResponse.receivedResponseAtMillis(); Headers headers = cacheResponse.headers(); for (int i = 0, size = headers.size(); i < size; i++) { String fieldName = headers.name(i); String value = headers.value(i); if ("Date".equalsIgnoreCase(fieldName)) { servedDate = HttpDate.parse(value); servedDateString = value; } else if ("Expires".equalsIgnoreCase(fieldName)) { expires = HttpDate.parse(value); } else if ("Last-Modified".equalsIgnoreCase(fieldName)) { lastModified = HttpDate.parse(value); lastModifiedString = value; } else if ("ETag".equalsIgnoreCase(fieldName)) { etag = value; } else if ("Age".equalsIgnoreCase(fieldName)) { ageSeconds = HttpHeaders.parseSeconds(value, -1); } } } }
Example #12
Source File: Response.java From styT with Apache License 2.0 | 5 votes |
/** * Returns the authorization challenges appropriate for this response's code. If the response code * is 401 unauthorized, this returns the "WWW-Authenticate" challenges. If the response code is * 407 proxy unauthorized, this returns the "Proxy-Authenticate" challenges. Otherwise this * returns an empty list of challenges. */ public List<Challenge> challenges() { String responseField; if (code == HTTP_UNAUTHORIZED) { responseField = "WWW-Authenticate"; } else if (code == HTTP_PROXY_AUTH) { responseField = "Proxy-Authenticate"; } else { return Collections.emptyList(); } return HttpHeaders.parseChallenges(headers(), responseField); }
Example #13
Source File: Cache.java From styT with Apache License 2.0 | 5 votes |
public Entry(Response response) { this.url = response.request().url().toString(); this.varyHeaders = HttpHeaders.varyHeaders(response); this.requestMethod = response.request().method(); this.protocol = response.protocol(); this.code = response.code(); this.message = response.message(); this.responseHeaders = response.headers(); this.handshake = response.handshake(); this.sentRequestMillis = response.sentRequestAtMillis(); this.receivedResponseMillis = response.receivedResponseAtMillis(); }
Example #14
Source File: OkHttpInterceptor.java From pandora with Apache License 2.0 | 5 votes |
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 #15
Source File: ResponseAdapter.java From scava with Eclipse Public License 2.0 | 4 votes |
public ResponseAdapter(Response response){ this.contentLength = HttpHeaders.contentLength(response.headers()); this.contentType = response.headers().get(CONTENT_TYPE); this.response = response; }
Example #16
Source File: PcGetQueueCountCrawler.java From MyBlog with Apache License 2.0 | 4 votes |
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; }
Example #17
Source File: Cache.java From AndroidProjects with MIT License | 4 votes |
public boolean matches(Request request, Response response) { return url.equals(request.url().toString()) && requestMethod.equals(request.method()) && HttpHeaders.varyMatches(response, varyHeaders, request); }
Example #18
Source File: RealConnection.java From styT with Apache License 2.0 | 4 votes |
/** * To make an HTTPS connection over an HTTP proxy, send an unencrypted CONNECT request to create * the proxy connection. This may need to be retried if the proxy requires authorization. */ private Request createTunnel(int readTimeout, int writeTimeout, Request tunnelRequest, HttpUrl url) throws IOException { // Make an SSL Tunnel on the first message pair of each SSL + proxy connection. String requestLine = "CONNECT " + Util.hostHeader(url, true) + " HTTP/1.1"; while (true) { Http1Codec tunnelConnection = new Http1Codec(null, null, source, sink); source.timeout().timeout(readTimeout, MILLISECONDS); sink.timeout().timeout(writeTimeout, MILLISECONDS); tunnelConnection.writeRequest(tunnelRequest.headers(), requestLine); tunnelConnection.finishRequest(); Response response = tunnelConnection.readResponseHeaders(false) .request(tunnelRequest) .build(); // The response body from a CONNECT should be empty, but if it is not then we should consume // it before proceeding. long contentLength = HttpHeaders.contentLength(response); if (contentLength == -1L) { contentLength = 0L; } Source body = tunnelConnection.newFixedLengthSource(contentLength); Util.skipAll(body, Integer.MAX_VALUE, TimeUnit.MILLISECONDS); body.close(); switch (response.code()) { case HTTP_OK: // Assume the server won't send a TLS ServerHello until we send a TLS ClientHello. If // that happens, then we will have buffered bytes that are needed by the SSLSocket! // This check is imperfect: it doesn't tell us whether a handshake will succeed, just // that it will almost certainly fail because the proxy has sent unexpected data. if (!source.buffer().exhausted() || !sink.buffer().exhausted()) { throw new IOException("TLS tunnel buffered too many bytes!"); } return null; case HTTP_PROXY_AUTH: tunnelRequest = route.address().proxyAuthenticator().authenticate(route, response); if (tunnelRequest == null) throw new IOException("Failed to authenticate with proxy"); if ("close".equalsIgnoreCase(response.header("Connection"))) { return tunnelRequest; } break; default: throw new IOException( "Unexpected response code for CONNECT: " + response.code()); } } }
Example #19
Source File: Cache.java From styT with Apache License 2.0 | 4 votes |
public boolean matches(Request request, Response response) { return url.equals(request.url().toString()) && requestMethod.equals(request.method()) && HttpHeaders.varyMatches(response, varyHeaders, request); }
Example #20
Source File: RealConnection.java From AndroidProjects with MIT License | 4 votes |
/** * To make an HTTPS connection over an HTTP proxy, send an unencrypted CONNECT request to create * the proxy connection. This may need to be retried if the proxy requires authorization. */ private Request createTunnel(int readTimeout, int writeTimeout, Request tunnelRequest, HttpUrl url) throws IOException { // Make an SSL Tunnel on the first message pair of each SSL + proxy connection. String requestLine = "CONNECT " + Util.hostHeader(url, true) + " HTTP/1.1"; while (true) { Http1Codec tunnelConnection = new Http1Codec(null, null, source, sink); source.timeout().timeout(readTimeout, MILLISECONDS); sink.timeout().timeout(writeTimeout, MILLISECONDS); tunnelConnection.writeRequest(tunnelRequest.headers(), requestLine); tunnelConnection.finishRequest(); Response response = tunnelConnection.readResponseHeaders(false) .request(tunnelRequest) .build(); // The response body from a CONNECT should be empty, but if it is not then we should consume // it before proceeding. long contentLength = HttpHeaders.contentLength(response); if (contentLength == -1L) { contentLength = 0L; } Source body = tunnelConnection.newFixedLengthSource(contentLength); Util.skipAll(body, Integer.MAX_VALUE, TimeUnit.MILLISECONDS); body.close(); switch (response.code()) { case HTTP_OK: // Assume the server won't send a TLS ServerHello until we send a TLS ClientHello. If // that happens, then we will have buffered bytes that are needed by the SSLSocket! // This check is imperfect: it doesn't tell us whether a handshake will succeed, just // that it will almost certainly fail because the proxy has sent unexpected data. if (!source.buffer().exhausted() || !sink.buffer().exhausted()) { throw new IOException("TLS tunnel buffered too many bytes!"); } return null; case HTTP_PROXY_AUTH: tunnelRequest = route.address().proxyAuthenticator().authenticate(route, response); if (tunnelRequest == null) throw new IOException("Failed to authenticate with proxy"); if ("close".equalsIgnoreCase(response.header("Connection"))) { return tunnelRequest; } break; default: throw new IOException( "Unexpected response code for CONNECT: " + response.code()); } } }