1

would like to post a huge JSON object, but when i try i got this error:

java.net.SocketException: Connection reset by peer: socket write error
 at java.net.SocketOutputStream.socketWrite0(Native Method)
 at java.net.SocketOutputStream.socketWrite(Unknown Source)
 at java.net.SocketOutputStream.write(Unknown Source)
 at org.apache.http.impl.io.SessionOutputBufferImpl.streamWrite(SessionOutputBufferImpl.java:126)
 at org.apache.http.impl.io.SessionOutputBufferImpl.write(SessionOutputBufferImpl.java:162)
 at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:115)
 at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:122)
 at org.apache.http.entity.StringEntity.writeTo(StringEntity.java:169)
 at org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity(DefaultBHttpClientConnection.java:158)
 at org.apache.http.impl.conn.CPoolProxy.sendRequestEntity(CPoolProxy.java:162)
 at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:237)
 at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:122)
 at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
 at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
 at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
 at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
 at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
 at com.os.util.helper.microServiceHelper.AbstractMicroServiceHelper.getResultFromMicroServices(AbstractMicroServiceHelper.java:196)
 at com.os.util.helper.microServiceHelper.IsheetVersionMicroserviceImpl.bulkInsertUpdateIsheetVersion(IsheetVersionMicroserviceImpl.java:75)
 at com.os.gfnactions.isheet.IsheetVersionPutInMicroserviceThread.run(IsheetVersionPutInMicroserviceThread.java:36)
 at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
 at java.util.concurrent.FutureTask.run(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
 at java.lang.Thread.run(Unknown Source)

Socket Exception

When I am sending a large heavy json through HttpClient Post request it will give me a below error.

so how to solve below error i was study all posted question.but doesn't get a proper solution for my error.

FYI : Basically we are use spring boot api at server side. we are call that api from a application which is in java.

can you please anyone help me solve below error??

1) How to send from client side in java ?

2) How to get server side that json in java ?

Tejas Shah
  • 11
  • 3
  • I think posting huge text it's not good idea. I propouse you should split this json and apply some pagination for this. – lukassz Apr 26 '17 at 15:07
  • did you try increasing timeout ? – pvpkiran Apr 26 '17 at 15:08
  • @lukassz do you have any other way without split a json because we already split that json in small json actually original json size is almost 50 MB. – Tejas Shah Apr 26 '17 at 15:13
  • @pvpkiran we never use timeout.how to use timeout??? .how its works?? – Tejas Shah Apr 26 '17 at 15:13
  • As @lukassz said it is not a good idea to send huge files. But stil you can check this to increase timeout. http://stackoverflow.com/questions/3000214/java-http-client-request-with-defined-timeout you can set different types of timeout – pvpkiran Apr 26 '17 at 15:48
  • I too faced the similar problem with my sprint boot app, you might need to increase the max size of the upload file as mentioned on https://stackoverflow.com/questions/37540028/how-to-set-the-max-size-of-upload-file – Jeet Prakash May 09 '21 at 19:29
  • Instead of sending it as text file, you could send it as binary file as N chunk of P bytes, and use sequence/index (eg: send chunks of file, then one order to rearrange those chunk). That would remove the timeout/limitation. – NoDataFound May 09 '21 at 19:36

2 Answers2

0

You should try increase socket timeout by typing socket.setSoTimeout(number)

Documentation: https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#setSoTimeout(int)

Posting huge text it's not good idea. You should apply some pagination for this and split eg. every 200kb.

For big json files I recommend BSON http://bsonspec.org/

lukassz
  • 3,135
  • 7
  • 32
  • 72
0

One of the reason it might happen is because of the request size limitation on the tomcat server. In that case you will need to increase that as mentioned on How to set the max size of upload file

Jeet Prakash
  • 625
  • 1
  • 7
  • 13