简体   繁体   English

是否可以使用普通套接字连接发送HTTP请求并接收不带标题的响应?

[英]Is it possible to send HTTP request using plain socket connection and receive response without headers?

experts 专家

I am trying to implement a downloader which handles HTTP protocol, I am able to send HTTP request using the following code: 我正在尝试实现一个处理HTTP协议的下载器,我能够使用以下代码发送HTTP请求:

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. 收到响应后,我需要将响应的主体保存到磁盘,然后完成工作。

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. 我问这个问题的原因是,如果可能的话,在读取响应时不需要手动跳过HTTP标头,则可以直接开始从输入流中读取原始字节。 all I want is the body of the response, the HTTP headers don't matter much in this case. 我想要的只是响应的主体,在这种情况下,HTTP标头并不重要。

If you don't control the server then probably not. 如果您不控制服务器,则可能不会。 If you do, then depending on what you're using server side you might be able to send a response without headers. 如果这样做,则取决于您使用服务器端的内容,您也许可以发送不带标题的响应。

It looks like you're using Java and I suspect there's libraries to help you extract the response without parsing it yourself. 看来您使用的是Java,而且我怀疑有库可以帮助您提取响应而不自己解析。 That would be better than trying to hack together a response without headers. 这比尝试合并没有标题的响应要好。

that is the HTTP protocol is working. HTTP协议正在工作。 You always get headers in response. 您总是会得到标题作为响应。 I am pretty sure, Java has a ready to use http client somewhere. 我很确定,Java在某个地方可以使用http客户端。 i suggest you use it. 我建议您使用它。

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. 而且,由于要尝试检索的资源可能采用不同的编码和/或多个部分,因此您必须对其进行解析。

On the other hand, this has been done for you already by tools like wget(1) . 另一方面,已经通过wget(1)类的工具为您完成了此操作。 You can either just use them, or look into the source to figure out what you need to do programmatically. 您可以只使用它们,也可以查看源代码来找出您需要以编程方式进行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以发送输出流作为对 HTTP 请求的响应? - Is it possible to send an ouput stream as response to a HTTP request? Java套接字将发送但不接收响应 - Java socket will send but then not receive response http是否可以发送流式响应而不是“ Range”响应/请求? - Can http send a streamed response without it being a “Range” response/request? 向JSP发送请求并接收响应 - Send request to JSP and receive response 如何使用 Java Mail 发送 iCal 会议请求并接收响应 - How to send an iCal meeting request using Java Mail, and receive the response Android-使用Volley显示自定义列表视图(发送带有HTTP请求的标头) - Android - Using Volley to display a custom listview (send headers with the http request) 通过android中的http连接发送和接收字符串 - send and receive strings through http connection in android 想要只在我的输出中打印正文部分而不是http标题。.Http请求是使用套接字编程处理的 - Want to print only body part in my output not the http headers.. Http Request is handled using socket programming 如何在HTTP请求标头中发送签名 - How to send signature in http request headers 使用骆驼在HTTP上发送普通肥皂 - Send plain soap over http using camel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM