Java:使用HttpURLConnection发送请求时报文体为什么会为空?
2021/11/28 20:43:05
本文主要是介绍Java:使用HttpURLConnection发送请求时报文体为什么会为空?,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
最近使用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源代码:
https://code.yawk.at/java/8/sun/net/www/protocol/http/HttpURLConnection.java#sun.net.www.protocol.http.HttpURLConnection%23getInputStream0()
这篇关于Java:使用HttpURLConnection发送请求时报文体为什么会为空?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-09百万架构师第十二课:源码分析:Spring 源码分析:Spring系统概述及IOC实现原理|JavaGuide
- 2025-01-08如何用关键链方法突破项目管理瓶颈?
- 2025-01-08电商人必看!6 款提升团队协作与客户满意度软件!
- 2025-01-08电商团队管理混乱?快用这 6 款软件优化协作流程!
- 2025-01-08短剧制作效率低?试试这5款任务管理工具
- 2025-01-08高效应对电商高峰,6 款团队协作软件大揭秘!
- 2025-01-08为什么外贸人都爱上了在线协作工具?
- 2025-01-08提升工作效率,从这些任务管理工具开始
- 2025-01-08新年电商订单暴增,必备的 6 款可视化协作办公软件有哪些?
- 2025-01-08短剧制作经理必备技能与工具全解析