3

有趣的是:我可能不小心找到了解决方案是否可以使用普通套接字连接发送 HTTP 请求并接收没有标头的响应?. 我希望我忽略了一些东西。

无论如何,我正在连接到 Web 服务器,发送 HTTP 请求,接收响应并关闭连接。实际的 HTTP 会话(服务器端的 tcpdump)如下所示:

GET /blah.php?param1=test1t&param2=test2 HTTP/1.0

HTTP/1.1 200 OK
Date: Sat, 22 Sep 2012 10:16:46 GMT
Server: Apache/2.2.17 (Ubuntu)
X-Powered-By: PHP/5.3.5-1ubuntu7.8
Vary: Accept-Encoding
Content-Length: 17
Connection: close
Content-Type: text/html

<pre>173364</pre>

凉爽的。它工作......到目前为止。现在这是代码。字符串 szRetval 仅包含“<pre>173364</pre>”。

Socket s = new Socket("1.2.3.4", 80);
//DataInputStream in = new DataInputStream(s.getInputStream());
BufferedReader bin = new BufferedReader(new InputStreamReader(s.getInputStream()));
DataOutputStream out = new DataOutputStream(s.getOutputStream());

out.writeBytes(szRequest);
String szRetval = "";
String szLine = "";
while((szLine=bin.readLine())!=null) {
    szRetval += szLine;
}
s.close();
return szRetval;

从代码示例中可以看出,我已经从 DataInputStream 切换到了 BufferedReader。没有区别,所以我想这与 Socket 类本身有关。

为什么o为什么(原始)套接字不返回http响应标头?

Android 2.3.3,AsyncTask 中的网络 I/O,不使用代理。

4

0 回答 0