gpt4 book ai didi

java - 为什么除非我们从服务器读取 POST 响应,否则 HTTP POST 不起作用?

转载 作者:行者123 更新时间:2023-12-02 07:33:42 25 4
gpt4 key购买 nike

我正在为一个网站编写一个 uploader 类,在上传文件后,我正在读取该网站的上传响应。如果我没有阅读响应,则文件不会上传。我的代码如下,

   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)) > 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, "<link>", "</link>");
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();
}
}

如您所见,我正在以下行中将数据写入服务器,

for (int length = 0; (length = input.read(buffer)) > 0;) {

output.write(buffer, 0, length);
}

但是在此之后,我必须执行以下操作,

 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String k = "", tmp = "";
while ((tmp = br.readLine()) != null) {
System.out.println(tmp);
k += tmp;
}

为什么我必须读取服务器的响应才能上传成功?

有人能解释一下吗?

提前致谢。

最佳答案

如果您尝试向服务器发布某些内容,并且在完全发送消息之前连接中断,则服务器将停止处理不完整的请求。一旦服务器完成接收请求并且您不等待得到其响应,那么您将永远不知道您的请求是否发送成功。因此,URLConnection 被设计为等待您收到响应。

另一个原因是您可能需要先构建和配置 URLConnection,然后在需要时通过调用 getInputStreamgetResponseCode 来发送它。您可以更好地控制何时进行交易。

您不需要总是getInputStream,只需调用getResponseCode就足以完成请求。但是,整个输入流仍会发送到您的代码,但它们会被丢弃。

关于java - 为什么除非我们从服务器读取 POST 响应,否则 HTTP POST 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12559058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com