I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this.When I use okhttp
or httpclient
to post a big data, a java.net.SocketException is throwed.The big data is a big string which is is encoded as base64 from a 52M size file.I conducted capture with wireshark
, but nothing.However when I used postman
to do this, it worked.
I increased the max-http-post-size
and the connectTimeout, soTimeout, but it was useless.
The server is only a simple post restful interface which built by spring boot.
The client code is:
String text = ConvertUtil.convertTimeBetaTo2017(video.toJSONString());
final DigestAuthenticator authenticator = new DigestAuthenticator(
new Credentials(this.serverInfo.getUsername(), this.serverInfo.getPassword())
);
final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
final OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES)
.readTimeout(10, TimeUnit.MINUTES)
.writeTimeout(20, TimeUnit.MINUTES)
.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache))
.addInterceptor(new AuthenticationCacheInterceptor(authCache))
.build();
String url = this.serverInfo.getServer() + VbankRestfulUri.VideoSlices.getValue();
final MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
Request request = new Request.Builder()
.url(url)
.addHeader("client-info", this.serverInfo.getClientInfo())
.addHeader("Connection", "Keep-Alive")
.addHeader("Transfer-Encoding", "chunked")
.addHeader("X-Content-Type-Options", "nosniff")
.addHeader("X-XSS-Protection", "1; mode=block")
.addHeader("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
.addHeader("Pragma", "no-cache")
.addHeader("Expires", "0")
.post(RequestBody.create(text, mediaType))
.build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return new Result(true, "操作成功");
}
else {
return new Result(false, "操作失败");
}
} catch (IOException e) {
e.printStackTrace();
return new Result(false, e.getMessage());
}
The exception is :
java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
at okio.OutputStreamSink.write(Okio.kt:65)
at okio.AsyncTimeout$sink$1.write(AsyncTimeout.kt:106)
at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.kt:181)
at okio.RealBufferedSink.write(RealBufferedSink.kt:39)
at okhttp3.internal.http1.Http1ExchangeCodec$KnownLengthSink.write(Http1ExchangeCodec.kt:292)
at okio.ForwardingSink.write(ForwardingSink.kt:29)
at okhttp3.internal.connection.Exchange$RequestBodySink.write(Exchange.kt:222)
at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.kt:181)
at okio.RealBufferedSink.write(RealBufferedSink.kt:92)
at okhttp3.RequestBody$Companion$toRequestBody$2.writeTo(RequestBody.kt:147)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:59)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:37)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at com.burgstaller.okhttp.AuthenticationCacheInterceptor.intercept(AuthenticationCacheInterceptor.java:49)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
at okhttp3.RealCall.execute(RealCall.kt:66)