Java教程

Java:使用HttpURLConnection发送请求时报文体为什么会为空?

本文主要是介绍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()

这篇关于Java:使用HttpURLConnection发送请求时报文体为什么会为空?的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!