Is it possible to send HTTP request using plain so

2019-08-27 19:42发布

问题:

experts

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.

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)?

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.

回答1:

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). You can either just use them, or look into the source to figure out what you need to do programmatically.



回答2:

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. That would be better than trying to hack together a response without headers.



回答3:

that is the HTTP protocol is working. You always get headers in response. I am pretty sure, Java has a ready to use http client somewhere. i suggest you use it.