gpt4 book ai didi

Java HTTP 响应仅包含 header

转载 作者:可可西里 更新时间:2023-11-01 02:44:58 26 4
gpt4 key购买 nike

我正在通过 TCP 套接字发送一个 HTTP 请求,并且我得到了 header 作为响应,尽管内容只包含一个问号。那是什么?

这是我的代码:

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();
}

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

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

?

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

最佳答案

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

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

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

关于Java HTTP 响应仅包含 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11466326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com