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: 12

hello 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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值