Java HTTP响应仅包含标头

时间:2012-07-13 07:52:07

标签: java http sockets tcp

我通过TCP套接字发送HTTP请求,并且我将标头作为响应,但内容只包含问号。那是什么?

这是我的代码:

Socket sock = null;
OutputStream out = null;
InputStream in = null;

try {
    // open socket
    sock = new Socket(this.addr, this.port);

    // get output stream
    out = sock.getOutputStream();

    // create request
    StringBuffer request = new StringBuffer();
    request.append("GET " + this.uri + " HTTP/1.1").append(this.CRLF);
    request.append("Host: " + this.host).append(this.CRLF);
    request.append("Cache-Control: no-cache").append(this.CRLF);
    request.append("Connection: keep-alive").append(this.CRLF);
    request.append("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11").append(this.CRLF);
    request.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8").append(this.CRLF);
    request.append("Accept-Encoding: gzip,deflate,sdch").append(this.CRLF);
    request.append("Accept-Language: en-GB").append(this.CRLF);
    request.append("Accept-Language: ISO-8859-1,utf-8;q=0.7,*;q=0.3").append(this.CRLF);
    request.append("Pragma: no-cache").append(this.CRLF);
    request.append(this.CRLF);

    // write request per byte for the lulz
    for(int i = 0; i < request.length(); i++) {
        out.write(request.toString().getBytes()[i]);
        System.out.print((char) request.toString().getBytes()[i]);
    }

    out.flush();

    // open inputstream
    in = sock.getInputStream();

    int inbyte;

    // read response per byte for the lulz
    while((inbyte = in.read()) > 0) {
        System.out.print((char) inbyte);
    }

    // close out, in and socket
    out.close();
    in.close();
    sock.close();
} catch (IOException e) {
    e.printStackTrace();
}

您可以看到我的请求标题,但这里是实际输出:

GET / HTTP/1.1
Host: www.timseverien.nl
Cache-Control: no-cache
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB
Accept-Language: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Pragma: no-cache

最后,回应:

HTTP/1.1 200 OK
Date: Fri, 13 Jul 2012 07:47:25 GMT
Server: Apache/2
X-Pingback: http://www.timseverien.nl/xmlrpc.php
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 2758
Keep-Alive: timeout=1, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

?

为什么我会收到此问号而不是源代码?

2 个答案:

答案 0 :(得分:4)

Content-Encoding: gzip - 您的回复已被压缩,无法可靠地打印到屏幕上。

从标题中删除Accept-Encoding,您应该会收到纯文本。

如果您想使用http,请以http / 1.0开头。处理起来要容易得多。

答案 1 :(得分:1)

正如Banthar已经指出的那样,内容被gzip压缩,但内容长度为2758字节,但你只读了1.

InputStream.read()的javadocs说:

  

如果b的长度为零,则不读取任何字节,返回0;   否则,尝试读取至少一个字节。如果没有字节   是可用的,因为流是在文件的末尾,   值-1返回;否则,至少读取一个字节并存储到b。

我认为你的测试是&gt; 0是错的。

while((inbyte = in.read()) > 0) {

应该是:

while((inbyte = in.read()) >= 0) {

in.read()可能返回0-255范围内的值(一个字节的整个范围)。当没有更多数据可用时,in.read()将返回-1。这就是in.read()返回int而不是byte

的原因