Search in sources :

Example 1 with ProgressBody

use of org.xutils.http.body.ProgressBody in project xUtils3 by wyouflf.

the class HttpRequest method sendRequest.

/**
     * invoke via Loader
     *
     * @throws IOException
     */
@Override
@TargetApi(Build.VERSION_CODES.KITKAT)
public void sendRequest() throws Throwable {
    isLoading = false;
    responseCode = 0;
    URL url = new URL(queryUrl);
    {
        // init connection
        Proxy proxy = params.getProxy();
        if (proxy != null) {
            connection = (HttpURLConnection) url.openConnection(proxy);
        } else {
            connection = (HttpURLConnection) url.openConnection();
        }
        // try to fix bug: accidental EOFException before API 19
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            connection.setRequestProperty("Connection", "close");
        }
        connection.setReadTimeout(params.getReadTimeout());
        connection.setConnectTimeout(params.getConnectTimeout());
        connection.setInstanceFollowRedirects(params.getRedirectHandler() == null);
        if (connection instanceof HttpsURLConnection) {
            SSLSocketFactory sslSocketFactory = params.getSslSocketFactory();
            if (sslSocketFactory != null) {
                ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
            }
        }
    }
    if (params.isUseCookie()) {
        // add cookies
        try {
            Map<String, List<String>> singleMap = COOKIE_MANAGER.get(url.toURI(), new HashMap<String, List<String>>(0));
            List<String> cookies = singleMap.get("Cookie");
            if (cookies != null) {
                connection.setRequestProperty("Cookie", TextUtils.join(";", cookies));
            }
        } catch (Throwable ex) {
            LogUtil.e(ex.getMessage(), ex);
        }
    }
    {
        // add headers
        List<RequestParams.Header> headers = params.getHeaders();
        if (headers != null) {
            for (RequestParams.Header header : headers) {
                String name = header.key;
                String value = header.getValueStr();
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(value)) {
                    if (header.setHeader) {
                        connection.setRequestProperty(name, value);
                    } else {
                        connection.addRequestProperty(name, value);
                    }
                }
            }
        }
    }
    // intercept response
    if (requestInterceptListener != null) {
        requestInterceptListener.beforeRequest(this);
    }
    {
        // write body
        HttpMethod method = params.getMethod();
        try {
            connection.setRequestMethod(method.toString());
        } catch (ProtocolException ex) {
            try {
                // fix: HttpURLConnection not support PATCH method.
                Field methodField = HttpURLConnection.class.getDeclaredField("method");
                methodField.setAccessible(true);
                methodField.set(connection, method.toString());
            } catch (Throwable ignored) {
                throw ex;
            }
        }
        if (HttpMethod.permitsRequestBody(method)) {
            RequestBody body = params.getRequestBody();
            if (body != null) {
                if (body instanceof ProgressBody) {
                    ((ProgressBody) body).setProgressHandler(progressHandler);
                }
                String contentType = body.getContentType();
                if (!TextUtils.isEmpty(contentType)) {
                    connection.setRequestProperty("Content-Type", contentType);
                }
                long contentLength = body.getContentLength();
                if (contentLength < 0) {
                    connection.setChunkedStreamingMode(256 * 1024);
                } else {
                    if (contentLength < Integer.MAX_VALUE) {
                        connection.setFixedLengthStreamingMode((int) contentLength);
                    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                        connection.setFixedLengthStreamingMode(contentLength);
                    } else {
                        connection.setChunkedStreamingMode(256 * 1024);
                    }
                }
                connection.setRequestProperty("Content-Length", String.valueOf(contentLength));
                connection.setDoOutput(true);
                body.writeTo(connection.getOutputStream());
            }
        }
    }
    if (params.isUseCookie()) {
        // save cookies
        try {
            Map<String, List<String>> headers = connection.getHeaderFields();
            if (headers != null) {
                COOKIE_MANAGER.put(url.toURI(), headers);
            }
        } catch (Throwable ex) {
            LogUtil.e(ex.getMessage(), ex);
        }
    }
    // check response code
    responseCode = connection.getResponseCode();
    // intercept response
    if (requestInterceptListener != null) {
        requestInterceptListener.afterRequest(this);
    }
    if (responseCode == 204 || responseCode == 205) {
        // empty content
        throw new HttpException(responseCode, this.getResponseMessage());
    } else if (responseCode >= 300) {
        HttpException httpException = new HttpException(responseCode, this.getResponseMessage());
        try {
            httpException.setResult(IOUtil.readStr(this.getInputStream(), params.getCharset()));
        } catch (Throwable ignored) {
        }
        LogUtil.e(httpException.toString() + ", url: " + queryUrl);
        throw httpException;
    }
    isLoading = true;
}
Also used : ProtocolException(java.net.ProtocolException) ProgressBody(org.xutils.http.body.ProgressBody) URL(java.net.URL) RequestParams(org.xutils.http.RequestParams) Field(java.lang.reflect.Field) Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) List(java.util.List) HttpException(org.xutils.ex.HttpException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpMethod(org.xutils.http.HttpMethod) RequestBody(org.xutils.http.body.RequestBody) TargetApi(android.annotation.TargetApi)

Aggregations

TargetApi (android.annotation.TargetApi)1 Field (java.lang.reflect.Field)1 HttpURLConnection (java.net.HttpURLConnection)1 ProtocolException (java.net.ProtocolException)1 Proxy (java.net.Proxy)1 URL (java.net.URL)1 List (java.util.List)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 HttpException (org.xutils.ex.HttpException)1 HttpMethod (org.xutils.http.HttpMethod)1 RequestParams (org.xutils.http.RequestParams)1 ProgressBody (org.xutils.http.body.ProgressBody)1 RequestBody (org.xutils.http.body.RequestBody)1