有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java HTTP响应仅包含头

我通过TCPsocket发送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) 个答案

  1. # 1 楼答案

    正如Banthar已经指出的,内容是gzip的,但是内容长度是2758字节,但是您只读取了1

    InputStream的javadocs。读()说:

    If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

    我认为您对>;0是错误的

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

    应该是:

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

    in.read()可以返回0-255(包括0-255)范围内的值(字节的完整范围)。当没有更多可用数据时in.read()将返回-1。这就是in.read()返回int而不是byte的原因

  2. # 2 楼答案

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

    从标题中删除Accept-Encoding,您将收到纯文本

    如果您想使用http,请从http/1.0开始。处理起来容易多了