content java,java – 即使没有contentlength头,也从HTTP请求获取内容

我保留了完整性的原始答案,但我刚刚查看了

HTTP RFC (2616)第4.3节:

The presence of a message-body in a request is signaled by the inclusion of a Content-Length or Transfer-Encoding header field in the request’s message-headers. A message-body MUST NOT be included in a request if the specification of the request method (section 5.1.1) does not allow sending an entity-body in requests. A server SHOULD read and forward a message-body on any request; if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request.

因此,如果您没有内容长度,则必须具有Transfer-Encoding(如果您没有,则应以400状态响应以指示错误请求或411(“需要长度”)).那时,你做转移编码告诉你的:)

现在,如果您正在处理servlet API(或类似的HTTP API),它可能会为您处理所有这些 – 此时您可以使用下面的技术从流中读取,直到它不再产生数据,因为API会处理它(即它不仅仅是一个原始套接字流).

如果您可以向我们提供有关您的上下文的更多信息,那将有所帮助.

原始答案

如果没有内容长度,则表示内容一直持续到数据结束(套接字关闭时).

继续读取输入流(例如,将其写入ByteArrayOutputStream以存储它,或者可能是文件),直到InputStream.read返回-1.例如:

byte[] buffer = new byte[8192];

ByteArrayOutputStream output = new ByteArrayOutputStream();

int bytesRead;

while ((bytesRead = inputStream.read(buffer)) != -1)

{

output.write(buffer, 0, bytesRead);

}

// Now use the data in "output"

编辑:正如评论中指出的那样,客户端可能正在使用分块编码.通常,您正在使用的HTTP API应该为您处理此问题,但如果您正在处理原始套接字,则必须自己处理它.

关于这是一个请求(因此客户端无法关闭连接)的观点是一个有趣的 – 我认为客户端可能只是关闭发送部分,但我不知道如何映射到TCP中的任何内容在这一刻.我的低级网络知识并非如此.

如果这个答案结果是“绝对没用”,我会删除它……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要设置HttpURLConnection的请求content-length和host,可以按照以下步骤进行: 1. 首先,创建一个URL对象,用于指定要连接的URL: ``` URL url = new URL("http://www.example.com"); ``` 2. 然后,使用URL对象的openConnection()方法创建一个HttpURLConnection对象: ``` HttpURLConnection connection = (HttpURLConnection) url.openConnection(); ``` 3. 接下来,使用HttpURLConnection对象的setRequestProperty()方法设置请求内容,例如: ``` connection.setRequestProperty("Content-Length", "100"); connection.setRequestProperty("Host", "www.example.com"); ``` 其中,第一个参数是请求的名称,第二个参数是请求的值。 4. 最后,使用HttpURLConnection对象的connect()方法连接到指定的URL: ``` connection.connect(); ``` 完整示例代码如下: ``` import java.net.*; import java.io.*; public class HttpURLConnectionExample { public static void main(String[] args) throws Exception { URL url = new URL("http://www.example.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Length", "100"); connection.setRequestProperty("Host", "www.example.com"); connection.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); } } ``` 注意:请求的设置应该在connect()方法之前完成,否则将无效。另外,如果要发送POST请求,则需要设置请求方法为POST,然后使用OutputStream将请求体写入连接中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值