Skip to content

Instantly share code, notes, and snippets.

@karussell
Last active July 27, 2023 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karussell/82851e303ea7b3459b2dea01f18949f4 to your computer and use it in GitHub Desktop.
Save karussell/82851e303ea7b3459b2dea01f18949f4 to your computer and use it in GitHub Desktop.
// for more than 4K locations or for slow network you need to gzip the body
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(20, TimeUnit.SECONDS);
client.setReadTimeout(20, TimeUnit.SECONDS);
client.interceptors().add(new GzipRequestInterceptor());
....
class GzipRequestInterceptor implements Interceptor {
@Override
public com.squareup.okhttp.Response intercept(Interceptor.Chain chain) throws IOException {
com.squareup.okhttp.Request originalRequest = chain.request();
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
return chain.proceed(originalRequest);
}
com.squareup.okhttp.Request compressedRequest = originalRequest.newBuilder()
.header("Content-Encoding", "gzip")
.method(originalRequest.method(), forceContentLength(gzip(originalRequest.body())))
.build();
return chain.proceed(compressedRequest);
}
/**
* https://github.com/square/okhttp/issues/350
*/
private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
final Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
return new RequestBody() {
@Override
public MediaType contentType() {
return requestBody.contentType();
}
@Override
public long contentLength() {
return buffer.size();
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write(buffer.snapshot());
}
};
}
private RequestBody gzip(final RequestBody body) {
return new RequestBody() {
@Override
public MediaType contentType() {
return body.contentType();
}
@Override
public long contentLength() {
return -1; // We don't know the compressed length in advance!
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
}
class PostLargeProblemForOkHttp3 {
public static void main(String[] args) throws IOException {
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS).
readTimeout(30, TimeUnit.SECONDS).addInterceptor(new GzipRequestInterceptor3()).build();
okhttp3.Request req = new okhttp3.Request.Builder().url("https://graphhopper.com/api/1/vrp/optimize?key=" + KEY).
post(okhttp3.RequestBody.create(okhttp3.MediaType.parse("application/json"), new File("vrp.json"))).build();
System.out.println(client.newCall(req).execute().body().string());
}
static class GzipRequestInterceptor3 implements okhttp3.Interceptor {
@Override
public okhttp3.Response intercept(okhttp3.Interceptor.Chain chain) throws IOException {
okhttp3.Request originalRequest = chain.request();
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
return chain.proceed(originalRequest);
}
okhttp3.Request compressedRequest = originalRequest.newBuilder()
.header("Content-Encoding", "gzip")
.method(originalRequest.method(), forceContentLength(gzip(originalRequest.body())))
.build();
return chain.proceed(compressedRequest);
}
/**
* https://github.com/square/okhttp/issues/350
*/
private okhttp3.RequestBody forceContentLength(final okhttp3.RequestBody requestBody) throws IOException {
final Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
return new okhttp3.RequestBody() {
@Override
public okhttp3.MediaType contentType() {
return requestBody.contentType();
}
@Override
public long contentLength() {
return buffer.size();
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write(buffer.snapshot());
}
};
}
private okhttp3.RequestBody gzip(final okhttp3.RequestBody body) {
return new okhttp3.RequestBody() {
@Override
public okhttp3.MediaType contentType() {
return body.contentType();
}
@Override
public long contentLength() {
return -1; // We don't know the compressed length in advance!
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
body.writeTo(gzipSink);
gzipSink.close();
}
};
}
}
}
@febry46
Copy link

febry46 commented Jul 27, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment