Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy HTTP POST is not working unless we read the POST response from server?
    primarykey
    data
    text
    <p>I am writing a uploader class for a site and in that, after uploading the files, I am reading the upload response from that site. If I didn't read the response, then the file is not getting uploaded. My code is as follows,</p> <pre><code> String charset = "UTF-8"; File binaryFile = new File("C:\\TestVideoFile.flv"); String boundary = Long.toHexString(System.currentTimeMillis()); System.out.println(boundary);// Just generate some unique random value. String CRLF = "\r\n"; // Line separator required by multipart/form-data. URLConnection connection = new URL(UPLOAD_URL).openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); PrintWriter writer = null; try { OutputStream output = connection.getOutputStream(); writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important! writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF); writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF); writer.append("Content-Transfer-Encoding: binary").append(CRLF); writer.append(CRLF).flush(); InputStream input = null; try { input = new FileInputStream(binaryFile); long filelen = binaryFile.length(); System.out.println("Length : " + filelen); int dataRead = 0; byte[] buffer = new byte[1024]; for (int length = 0; (length = input.read(buffer)) &gt; 0;) { output.write(buffer, 0, length); } System.out.println("Now only terminating the file write loop"); output.flush(); // Important! Output cannot be closed. Close of writer will close output as well. } catch (Exception e) { System.out.println(e); } finally { if (input != null) { try { input.close(); } catch (IOException logOrIgnore) { System.out.println(logOrIgnore); } } } writer.append(CRLF).flush(); // CRLF is important! It indicates end of binary boundary. // End of multipart/form-data. writer.append("--" + boundary + "--").append(CRLF); System.out.println("Sending username"); // Send normal param. writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"user\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); writer.append(CRLF); writer.append(username).append(CRLF).flush(); System.out.println("Sending password"); writer.append("--" + boundary).append(CRLF); writer.append("Content-Disposition: form-data; name=\"password\"").append(CRLF); writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); writer.append(CRLF); writer.append(password).append(CRLF).flush(); System.out.println("Reading response from server"); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String k = "", tmp = ""; while ((tmp = br.readLine()) != null) { System.out.println(tmp); k += tmp; } if (k.contains("Successfully")) { System.out.println("File Uploaded successfully into PutLocker :)"); String downloadLink = parseResponse(k, "&lt;link&gt;", "&lt;/link&gt;"); System.out.println("Download Link : " + downloadLink); } else { System.out.println("Upload failed :("); } } catch (Exception e) { System.out.println(e); } finally { if (writer != null) { writer.close(); } } </code></pre> <p>As you can see, I am writing the data into the server in the following line,</p> <pre><code>for (int length = 0; (length = input.read(buffer)) &gt; 0;) { output.write(buffer, 0, length); } </code></pre> <p>But after this, I have to do the following,</p> <pre><code> BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String k = "", tmp = ""; while ((tmp = br.readLine()) != null) { System.out.println(tmp); k += tmp; } </code></pre> <p>Why do I have to read the response from server to make the upload successful? </p> <p>Can anybody explain on this?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload