1

I am getting this weird scenario whenever i make request using POST with request body for upload file, my header part won't contain anything except contentType, whenever i try to read response it gives me EOFException, below is my code any help would be greatly appreciated.

    public static Response executeFWUploadRequest(Context context, String fileName, String version,
                                              String checksum, File payload, int retry) {


    if (CommonUtils.isConnectedToWiFi(context)) {
        HttpURLConnection urlConnection = null;
        String boundary = "+++++";
        String LINE_FEED = "\n";
        try {
            URL url = new URL(CommonUtils.UPLOAD_URL);

            CommonUtils.writeLog("HTTP Client REQUEST URL = " + url);

            urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestMethod(METHOD_POST);
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty(Tag.CONTENT_TYPE_TAG, "multipart/form-data; boundary=" + boundary);
            urlConnection.setRequestProperty("Connection", "close");
            urlConnection.setConnectTimeout(TimeOut);
            urlConnection.setReadTimeout(TimeOut);

            OutputStream outputStream = urlConnection.getOutputStream();

            BufferedOutputStream dos = new BufferedOutputStream(outputStream);
            dos.flush();
            String partition = "--" + boundary;
            dos.write(LINE_FEED.getBytes());

            dos.write(partition.getBytes());
            dos.write(LINE_FEED.getBytes());
            String disp3 = "Content-Disposition: form-data; name=\"" + Tag.FILE_LENGTH_FIELD + "\"" + LINE_FEED + LINE_FEED + "value = \"" + payload.length() + "\"";
            dos.write(disp3.getBytes());
            dos.write(LINE_FEED.getBytes());

            dos.write(partition.getBytes());
            dos.write(LINE_FEED.getBytes());
            String dis1 = "Content-Disposition: form-data; name=\"" + Tag.VERSION_TAG + "\"" + LINE_FEED + LINE_FEED + "value = \"" + version + "\"";
            dos.write(dis1.getBytes());
            dos.write(LINE_FEED.getBytes());

            dos.write(partition.getBytes());
            dos.write(LINE_FEED.getBytes());
            String disp2 = "Content-Disposition: form-data; name=\"" + Tag.CHECKSUM_TAG + "\"" + LINE_FEED + LINE_FEED + "value = \"" + checksum + "\"";
            dos.write(disp2.getBytes());
            dos.write(LINE_FEED.getBytes());

            dos.write(partition.getBytes());
            dos.write(LINE_FEED.getBytes());
            String disp4 = "Content-Disposition: form-data; name=\"file \"; " + Tag.FILE_NAME_TAG + "=\"" + payload.getName() + "\"";
            dos.write(disp4.getBytes());
            dos.write(LINE_FEED.getBytes());
            String disp5 = Tag.CONTENT_TYPE_TAG + ": " + Tag.STREAM_CONTENT_TYPE_VALUE;
            dos.write(disp5.getBytes());
            dos.write(LINE_FEED.getBytes());
            dos.write(LINE_FEED.getBytes());

            FileInputStream inputStream = new FileInputStream(payload);
            byte[] buffer = new byte[4096];
            int bytesRead = -1;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                dos.write(buffer, 0, bytesRead);
            }

            dos.write(LINE_FEED.getBytes());
            dos.write((partition + "--").getBytes());
            dos.flush();
            dos.close();
            //tried with and with out below 2 lines
            outputStream.close();
            urlConnection.connect();
            //This line gives error its method to read response with its patch
            return readData(urlConnection,0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    } else {
        CommonUtils.writeLog("HTTP Client : Wifi connection is not available");
    }
    return null;
}




private static final int MAX_RETRY = 5;
private static Response readData(HttpURLConnection urlConnection, int retry) {
    Response objResponse = null;
    try {
        objResponse = new Response();
        String content;
        if (retry < MAX_RETRY) {
            objResponse.setResponseCode(urlConnection.getResponseCode());

            if (objResponse.getResponseCode() == 200) {
                content = convertInputStreamToString(new BufferedInputStream(urlConnection
                        .getInputStream()));
                objResponse.setResponseContent(content);
            } else {
                content = convertInputStreamToString(new BufferedInputStream(urlConnection
                        .getErrorStream()));
                objResponse.setResponseContent(content);
            }
        }
    } catch (EOFException eof) {
        eof.printStackTrace();
        readData(urlConnection, retry + 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return objResponse;
}

Here return type of Response is custom POJO class which store response code and response content.

Apar Amin
  • 653
  • 12
  • 22
  • Looks rather strange to have multiple `String disp* = "Content-Disposition: form-data; ` headers – Scary Wombat Jun 16 '16 at 04:27
  • well it just a variable name used for body part as BOS only allow to write byte which is being written by disp*.getBytes() and from my r&d related problem i can say multiple content-disposition allowed in request body. – Apar Amin Jun 16 '16 at 04:29
  • Sorry, what I mean is having multiple `Content-Disposition` headers in the HTTP Request. see http://stackoverflow.com/questions/13578428/duplicate-headers-received-from-server – Scary Wombat Jun 16 '16 at 04:33
  • also http://stackoverflow.com/questions/15599618/multiple-distinct-content-disposition-headers-received-from-server-in-jasperrepo – Scary Wombat Jun 16 '16 at 04:36
  • thanks scary let me give a try with header that what i understand from link with single "Content-Disposition" – Apar Amin Jun 16 '16 at 04:44
  • hi scary without luck still getting EOFException now i added every dis* into header separately and body only contain file data.. – Apar Amin Jun 16 '16 at 05:04
  • we can set multiple Content-Disposition so its definitely not an issues. – Apar Amin Jun 16 '16 at 05:29

0 Answers0