httpurlconnection post请求中如何能保证请求头与正文是一次发送

httpurlconnection post请求中正文是在获取inputstream的时候发送,那请求头是不是在outputsream关闭的时候发送呢,如何能保证他两是一起发送?

            // 读取客户端数据    
            InputStream socketIn=socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(socketIn));
            String msg=null;
            int content_Length = 0;
            while((msg=br.readLine())!=null){
                System.out.println(msg);
                if(msg.contains("Content-Length")){
                    content_Length=Integer.parseInt(msg.split(":")[1].trim());
                }
                if("".equals(msg)){
                    int readSize;  
                    char[] chars = null;  
                    chars = new char[content_Length];  
                    long length_tmp = content_Length;  
                    long index = 0;// start from zero  
                    while ((readSize = br.read(chars, (int) index, (int) length_tmp)) != -1) {
                        length_tmp -= readSize;  
                        if (length_tmp == 0) {  
                            break;  
                        }  
                        index = index + readSize;// 写入字符数组的offset(偏移量)  
                    } 
                    System.out.println(new String(chars));
                }
            }

可以去了解下http协议和tcp协议

个人认为:只要是一次请求,那肯定是一起发送的。

如果你没有用range断点续传,就是一次发送的,你可以用fiddler这个工具抓包证实这一点

httpurlconnection发送请求 服务端开启socket服务接收 就会发现正文是在获取inputstream的时候发送而报头是在outputstream关闭的时候发送的