http or https Upload - Android Network

Android examples for Network:HTTPS SSL

Description

http or https Upload

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;
import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class Main {
    private static Map<String, String> cookieCache = new HashMap<String, String>();
    private static SSLContext sslcontext;
    private static HostnameVerifier hostnameVerifier;

    public static String httpUpload(String url, Map<String, String> params,
            String fieldName, File... files) throws IOException {
        String boundary = "---------------------------";
        String endLine = "\r\n--" + boundary + "--\r\n";
        URL _url = new URL(url);
        String host = _url.getHost();
        HttpURLConnection conn = getHttpURLConnection(_url);
        conn.setConnectTimeout(3000);/*from w  ww . j av a  2s  .c  o  m*/
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        String cookie = cookieCache.get(host);
        if (cookie != null) {
            conn.setRequestProperty("Cookie", cookie);
        }
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        StringBuilder textEntity = new StringBuilder();
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, String> entry : params.entrySet()) {//?????????????????
                textEntity.append("--").append(boundary).append("\r\n");
                textEntity
                        .append("Content-Disposition: form-data; name=\"")
                        .append(entry.getKey()).append("\"\r\n\r\n");
                textEntity.append(entry.getValue());
                textEntity.append("\r\n");
            }
        }
        StringBuilder sb = new StringBuilder();
        long fileLength = 0;
        for (File file : files) {
            sb.append("--").append(boundary).append("\r\n");
            sb.append("Content-Disposition: form-data; name=\"")
                    .append(fieldName).append("\"; filename=\"")
                    .append(file.getName()).append("\"\r\n");
            sb.append("Content-Type: application/octet-stream\r\n\r\n");
            fileLength += file.length();
            sb.append("\r\n");
        }
        int textLength = sb.length();
        int textEntityLength = textEntity.length();
        conn.setRequestProperty(
                "Content-Length",
                String.valueOf(textLength + fileLength + endLine.length()
                        + textEntityLength));
        OutputStream out = conn.getOutputStream();
        out.write(textEntity.toString().getBytes());
        byte[] buffer = new byte[1024 * 5];
        for (File file : files) {
            sb.delete(0, sb.length());
            sb.append("--").append(boundary).append("\r\n");
            sb.append("Content-Disposition: form-data; name=\"")
                    .append(fieldName).append("\"; filename=\"")
                    .append(file.getName()).append("\"\r\n");
            sb.append("Content-Type: application/octet-stream\r\n\r\n");
            out.write(sb.toString().getBytes());
            FileInputStream fis = new FileInputStream(file);
            int len;
            while ((len = fis.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.write("\r\n".getBytes());
            out.flush();
            fis.close();
        }
        out.write(endLine.getBytes());
        out.flush();
        int responseCode = conn.getResponseCode();
        if (responseCode == 200) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            sb.delete(0, sb.length());
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
        }
        cookie = conn.getHeaderField("Set-Cookie");
        if (cookie != null) {
            cookieCache.put(host, cookie);
        }
        out.close();
        conn.disconnect();
        return sb.length() > 0 ? sb.toString() : null;
    }

    private static HttpURLConnection getHttpURLConnection(URL url)
            throws IOException {
        HttpURLConnection conn;
        if (url.toString().startsWith("https")) {
            HttpsURLConnection connection = (HttpsURLConnection) url
                    .openConnection();
            connection.setSSLSocketFactory(getSslcontext()
                    .getSocketFactory());
            connection.setHostnameVerifier(getHostnameVerifier());
            conn = connection;
        } else {
            conn = (HttpURLConnection) url.openConnection();
        }
        return conn;
    }

    private static SSLContext getSslcontext() {
        if (sslcontext == null) {
            TrustManager trustManager = new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        X509Certificate[] x509Certificates, String s)
                        throws CertificateException {

                }

                @Override
                public void checkServerTrusted(
                        X509Certificate[] x509Certificates, String s)
                        throws CertificateException {

                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            try {
                sslcontext = SSLContext.getInstance("TLS");
                sslcontext.init(null, new TrustManager[] { trustManager },
                        new java.security.SecureRandom());
            } catch (NoSuchAlgorithmException | KeyManagementException e) {
                e.printStackTrace();
            }
        }
        return sslcontext;
    }

    private static HostnameVerifier getHostnameVerifier() {
        if (hostnameVerifier == null) {
            hostnameVerifier = new HostnameVerifier() {
                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
            };
        }
        return hostnameVerifier;
    }
}

Related Tutorials