Upload file stream to remote server with okhttp3.14.4 cannot get response message when remote server refuse the request
Hi I am using okhttp3.14.4 with openfeign, I want to upload file with stream, and my remote file server behind the nginx proxy, when request body length over the nginx client_max_body_size limit, nginx will reject this request with 413 response:
<html>
<head>
<title>413 Request Entity Too Large</title>
</head>
<body bgcolor="white">
<center>
<h1>413 Request Entity Too Large</h1>
</center>
<hr>
<center>nginx/1.12.2</center>
</body>
</html>
But with okhttp client, I just got this exception:
2020-04-14 at 11:05:28 CST ERROR com.shukun.toc.frontservice.util.StreamRequestBody (execute-pool-1) - Stream Closed java.io.IOException: Stream Closed
at java.io.FileInputStream.readBytes(Native Method) ~[?:1.8.0_202]
at java.io.FileInputStream.read(FileInputStream.java:255) ~[?:1.8.0_202]
at okio.Okio$2.read(Okio.java:140) ~[okio-1.17.2.jar:?]
at okio.RealBufferedSink.writeAll(RealBufferedSink.java:105) ~[okio-1.17.2.jar:?]
at com.shukun.toc.frontservice.util.StreamRequestBody.writeTo(StreamRequestBody.java:50) ~[classes/:?]
at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:173) ~[okhttp-3.14.4.jar:?]
at okhttp3.MultipartBody.writeTo(MultipartBody.java:114) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:69) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142) ~[okhttp-3.14.4.jar:?]
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117) ~[okhttp-3.14.4.jar:?]
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229) ~[okhttp-3.14.4.jar:?]
at okhttp3.RealCall.execute(RealCall.java:81) ~[okhttp-3.14.4.jar:?]
at com.shukun.toc.frontservice.util.NetworkUtil.postUploadFileWithOkHttp(NetworkUtil.java:72) ~[classes/:?]
at com.shukun.toc.frontservice.util.NetworkUtil.postUploadFile(NetworkUtil.java:113) ~[classes/:?]
at com.shukun.toc.frontservice.service.SyncCommService.uploadFileTask(SyncCommService.java:129) ~[classes/:?]
at com.shukun.toc.frontservice.runner.TaskDealRunner.lambda$null$0(TaskDealRunner.java:64) ~[classes/:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_202]
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [?:1.8.0_202]
at java.util.concurrent.FutureTask.run(FutureTask.java) [?:1.8.0_202]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_202]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_202]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_202]
My request code:
public static String postUploadFileWithOkHttp(String url, RequestBody postBody, Headers headers) throws AppException {
Request request = new Request.Builder()
.headers(headers)
.url(url)
.post(postBody)
.build();
try (Response response = okHttpClient.newCall(request).execute()){
int responseCode = response.code();
if(responseCode == HttpStatus.OK.value()) {
ResponseBody body = response.body();
try(InputStream responseStream = (body != null ? body.byteStream() : StreamUtils.emptyInput())) {
String contentType = response.headers().get(HttpHeaders.CONTENT_TYPE);
MediaType mediaType = (StringUtils.hasLength(contentType) ? MediaType.parseMediaType(contentType) : null);
Charset charset = (mediaType != null && mediaType.getCharset() != null ? mediaType.getCharset() : StandardCharsets.UTF_8);
return StreamUtils.copyToString(responseStream, charset);
}
} else {
log.error("Reponse code:{}, content: {}", responseCode, response.toString());
throw new AppException(response.toString());
}
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new AppException(e.getMessage());
}
}
So my question is: how can I get the response http code and msg in this situation?
Can you run an example server we can test against somewhere? This isn't reproducible as is.
Otherwise post to stackoverflow to ask for usage help.
Apologies if this seems harsh, but the issue tracker isn't for usage questions and as developers our free time doesn't scale to handling such requests, when better forums like stackoverflow are here for exactly this purpose.