1

我正在通过 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

?

为什么我得到这个问号而不是源代码?

4

2 回答 2

4

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

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

如果你想玩 http,从 http/1.0 开始。它更容易处理。

于 2012-07-13T08:05:38.797 回答
1

正如 Banthar 已经指出的那样,内容已被压缩,但内容长度为 2758 字节,但您只阅读了 1 个字节。

InputStream.read() 的 javadocs 说:

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

我认为您对 > 0 的测试是错误的。

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

应该:

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

in.read()可能返回 0-255 范围内的值(包括一个字节的完整范围)。当没有更多数据可用时,in.read()将返回 -1。这就是为什么in.read()返回 aint而不是 a 的原因byte

于 2012-07-13T08:12:04.783 回答