改造2-SSLException代替413

时间:2018-06-27 07:34:02

标签: java android nginx retrofit2 http-status-code-413

我正在尝试将文件(230MB)发送到NGinx/1.10.2服务器。

服务器配置为最大可处理200MB。

我发送这样的文件:

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

我的改装超时:读/写/连接-200秒

我的日志:

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

现在,我期望状态代码为413的错误。不幸的是,我收到:

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

30秒后

如果我使用Postman发送此文件,则它的工作原理类似于魅力-几毫秒后为413。所以我不认为这是服务器问题

Retrofit version: 2.4.0
OkHttp version: 3.10.0

请求部分:

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));
}

1 个答案:

答案 0 :(得分: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);
    }
}

这应该有效:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(YOUR_HTTPS_URL)
    .setClient(getUnsafeOkHttpClient())
    .build();