Best approach: HTTP POST (multi-part) from Android to GAE

I would like to capture the camera image on Android and send it to Google App Engine, which will save the image in blob storage. It sounds simple enough, and I can get a multi-user POST for GAE, but for saving to the Blob repository the servlet returns an HTTP redirect (302). So I need a connection that can follow redirects after doing an HTTP POST. Here is the WISH code:

public static String sendPhoto(String myUrl, byte[] imageData) {
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    String pathToOurFile = "/data/file_to_send.jpg";
    String urlServer = myUrl;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    // Please follow redirects
    HttpURLConnection.setFollowRedirects(true);
    BufferedReader rd = null;
    StringBuilder sb = null;
    String line = null;
    try {
        URL url = new URL(urlServer);
        connection = (HttpURLConnection) url.openConnection();
        // Allow Inputs & Outputs
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        // I really want you to follow redirects
        connection.setInstanceFollowRedirects(true);
        connection.setConnectTimeout(10000); // 10 sec
        connection.setReadTimeout(10000); // 10 sec
        // Enable POST method
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type",
            "multipart/form-data;boundary=" + boundary);
        connection.connect();
        outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream
                .writeBytes("Content-Disposition: form-data; name="
                    + "\"file1\";filename=\""
                    + pathToOurFile + "\"" + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.write(imageData);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens
            + lineEnd);
        outputStream.flush();
        outputStream.close();
        rd = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        sb = new StringBuilder();
        while ((line = rd.readLine()) != null) {
            sb.append(line + '\n');
        }
        Log.i("Response: ", sb.toString());
        // Responses from the server (code and message)
        int serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();
        Log.i("Response: serverResponseCode:",
            String.valueOf(serverResponseCode));
        Log.i("Response: serverResponseMessage:", serverResponseMessage);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

As far as I try, this code will not follow the redirects. The input pair is always empty, and the answer is always 302. The image loads beautifully, though. If someone can tell me what I can do to get him to follow the redirect so that I can read the answer, I would really appreciate it.

, , . , , Apache HTTP Client, , .

+3
2

!

, HttpURLConnection. HttpClient HttpGet org.apache.http, Android SDK.

, , , , .

+4

, , . Location .

0

Source: https://habr.com/ru/post/1755088/


All Articles