最近使用java.net.HttpURLConnection做一些简单的测试程序,发现发送的报文体为空:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpURLConnectionTester {
private HttpURLConnection connectServerConn() throws IOException {
// set ServerServlet URL
String urlPath = "http://127.0.0.1"
+ ":7010" //
+ "/wtcapp" //
+ "/server";
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println("HttpURLConnection: " + conn.getClass());
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/plain");
conn.connect();
return conn;
}
public void doTest01() {
try {
HttpURLConnection conn = connectServerConn();
OutputStream output = conn.getOutputStream();
InputStream input = conn.getInputStream(); // (1)
output.write("hello ".getBytes());
output.flush();
Thread.sleep(3000);
output.write("world!".getBytes());
output.flush();
output.close();
// (2)
StringBuffer ret = new StringBuffer();
int r = input.read();
while (r != -1) {
ret.append((char) r);
r = input.read();
}
System.out.println("ret:" + ret);
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
HttpURLConnectionTester tester = new HttpURLConnectionTester();
tester.doTest01();
}
}
截取报文发现:
POST /wtcapp/server HTTP/1.1
Content-Type: text/plain
User-Agent: Java/1.8.0_261
Host: 127.0.0.1:7010
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 0
Content-Length为0,报文体为空。
经过研究发现,只要把(1)行的代码,移动到(2)处理,问题就解决了:
public void doTest01() {
try {
HttpURLConnection conn = connectServerConn();
OutputStream output = conn.getOutputStream();
output.write("hello ".getBytes());
output.flush();
Thread.sleep(3000);
output.write("world!".getBytes());
output.flush();
output.close();
InputStream input = conn.getInputStream();
StringBuffer ret = new StringBuffer();
int r = input.read();
while (r != -1) {
ret.append((char) r);
r = input.read();
}
System.out.println("ret:" + ret);
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
POST /wtcapp/server HTTP/1.1
Content-Type: text/plain
User-Agent: Java/1.8.0_261
Host: 127.0.0.1:7010
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 12hello world!
查看HttpURLConnection的源代码发现,原来conn.getInputStream();时,会将前面打开的OutputStream给close掉造成的。
java.net.HttpURLConnection源代码: