0

I'm trying to send a file (230MB) to NGinx/1.10.2 server.

The server is configured to handle max 200MB.

I send a file like this:

@Multipart
@POST("api/test")
Completable test(
        @PartMap Map<String, RequestBody> params,
        @Part MultipartBody.Part file
);

My retrofit timeouts: Read/Write/Connect - 200 SEC

My log:

--> POST https://testserver.com/api/test
Content-Type: multipart/form-data; boundary=4efbe174-e228-4dc7-aeec-d6dbe4ab4302
Content-Length: 230757873
Authorization: Bearer ...
--> END POST

Now I expect error with 413 status code. Unfortunately, I receive:

<-- HTTP FAILED: javax.net.ssl.SSLException: Write error: ssl=0x76c8f32400: I/O error during system call, Connection reset by peer

after 30 sec.

If I use Postman to send this file then it works like charm - 413 after few millis. So I don't think it is server problem

Retrofit version: 2.4.0
OkHttp version: 3.10.0

Request part:

public Completable test(File file) {
    Map<String, RequestBody> map = getInfo();

    RequestBody body = RequestBody.create(MediaType.parse(getMimeType(file.toString())), file);
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), body);

    return retrofit.create(Api.class)
            .flatMapCompletable(api -> api.test(map, fileToUpload));
}
Lau
  • 1,714
  • 1
  • 21
  • 44

1 Answers1

0
    public static OkHttpClient getUnsafeOkHttpClient() {

    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(
                    java.security.cert.X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(
                    java.security.cert.X509Certificate[] chain,
                    String authType) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[0];
            }
        } };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts,
                new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext
                .getSocketFactory();

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient = okHttpClient.newBuilder()
                .sslSocketFactory(sslSocketFactory)
                .hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).build();

        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

This should work:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(YOUR_HTTPS_URL)
    .setClient(getUnsafeOkHttpClient())
    .build();
  • Well... Thanks for the response, but... few things: 1) `sslSocketFactory` method is deprecated; 2) Trusting all is usually bad idea; 3) Even If I tried your solution - it's not helping. – Lau Jun 27 '18 at 12:49