是否可以使用普通套接字连接发送HTTP请求并接收没有标头的响应? [英] Is it possible to send HTTP request using plain socket connection and receive response without headers?

查看:143
本文介绍了是否可以使用普通套接字连接发送HTTP请求并接收没有标头的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

专家

我正在尝试实现一个处理HTTP协议的下载程序,我可以使用以下代码发送HTTP请求:

I am trying to implement a downloader which handles HTTP protocol, I am able to send HTTP request using the following code:

StringBuilder request = new StringBuffer().append("GET ").append(uri.getPath());
if(uri.getQuery() != null)
    request.append('?').append(uri.getQuery());
request.append(" HTTP/1.1\nHost: ").append(uri.getHost()).append("\nAccept: */*\n\n");

// send the request
outputStream.write(request.toString().getBytes("utf-8"));
outputStream.flush();

收到回复后,我需要将响应的正文保存到磁盘和作业中已经完成了。

On receiving the response, I will need to save the body of the response to disk, and the job is done.

我的问题是:我可以为请求设置任何HTTP头,这会导致服务器发送没有HTTP头的响应(只有正文)吗?

My question is: are there any HTTP headers that I can set for the request which will cause the server to send the response without HTTP headers(only the body)?

我问这个问题的原因是,如果可以,我在阅读时不需要手动跳过HTTP标题 响应,我可以直接从输入流开始读取原始字节。我想要的只是响应的主体,在这种情况下HTTP标头并不重要。

The reason why I am asking this question is that if that is possible, I don't need to skip the HTTP headers manually when reading the response, I can directly start reading raw bytes from the input stream. all I want is the body of the response, the HTTP headers don't matter much in this case.

推荐答案

不,你将永远得到回头。并且您将不得不解析它,因为您尝试检索的资源可能有不同的编码和/或多个部分。

No, you will always get the header back. And you will have to parse it since the resource you are trying to retrieve might come in different encodings and/or in multiple parts.

另一方面,这已经是已经通过 wget(1) 。您可以直接使用它们,也可以查看源代码以找出您需要以编程方式执行的操作。

On the other hand, this has been done for you already by tools like wget(1). You can either just use them, or look into the source to figure out what you need to do programmatically.

这篇关于是否可以使用普通套接字连接发送HTTP请求并接收没有标头的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆