gpt4 book ai didi

java - 在 android 中使用 HttpURLConnection 重用套接字

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

我想重新使用由 HttpURLConnection 创建的套接字。但是我发现当我调查从我的远程 Web 服务器传入/传出的原始数据包时,它总是会创建新的套接字。以下是我访问远程 Web 服务器的代码。

public String getFromServer() throws IOException
{
URL url = new URL("http://192.168.137.2/test.jsp");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=utf8");
http.setRequestProperty("Connection", "keep-alive");
http.setUseCaches(true);
http.setFixedLengthStreamingMode(0);

http.connect();
InputStream is = http.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));

int c;
StringBuffer buffer =new StringBuffer();
while((c=in.read())!=-1)
{
buffer.append((char)c);
}
Log.i("hv","debug3:"+buffer);
is.close();
return buffer.toString();
}

并调用该函数两次。

try {
java.net.CookieManager cookieManager = new java.net.CookieManager();
System.setProperty("http.keepAlive","true");
getFromServer();
getFromServer();
} catch (Exception e) {
e.printStackTrace();
}

如您所见我的代码,我的删除服务器 IP 是 192.168.137.2。我的设备 IP 是 192.168.3.117。当我尝试这段代码时,我预计服务器和设备之间只有一个 TCP 连接。但是设备中有两次不同 TCP 端口的 TCP 连接。以下是服务器中的tcpdump日志。

15:40:01.739295 IP 192.168.3.117.48427 > 192.168.137.2.80: Flags [S], seq 2864255093, win 14600, options [mss 1460,sackOK,TS val 15969200 ecr 0,nop,wscale 6], length 0
E..<..@.?.r....u.....+.P...u......9............
............
15:40:01.739322 IP 192.168.137.2.80 > 192.168.3.117.48427: Flags [S.], seq 3335569309, ack 2864255094, win 14480, options [mss 1460,sackOK,TS val 28276944 ecr 15969200,nop,wscale 7], length 0
..........,........u.P.+.......v..8.
..x.........
15:40:01.741673 IP 192.168.3.117.48427 > 192.168.137.2.80: Flags [.], ack 1, win 229, options [nop,nop,TS val 15969202 ecr 28276944], length 0
E..4..@.?.r5...u.....+.P...v........A......
......x.
15:40:01.742168 IP 192.168.3.117.48427 > 192.168.137.2.80: Flags [P.], seq 1:252, ack 1, win 229, options [nop,nop,TS val 15969203 ecr 28276944], length 251
E../..@.?.q9...u.....+.P...v...............
......x.GET /test.jsp HTTP/1.1
Content-type: application/x-www-form-urlencoded;charset=utf8
Connection: keep-alive
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.0.4; SHV-E210S Build/IMM76D)
Host: 192.168.137.2
Accept-Encoding: gzip
Content-Length: 0


15:40:01.742193 IP 192.168.137.2.80 > 192.168.3.117.48427: Flags [.], ack 252, win 122, options [nop,nop,TS val 28276945 ecr 15969203], length 0
......@.@.e........u.P.+.......q...z
..x.....
15:40:01.743685 IP 192.168.137.2.80 > 192.168.3.117.48427: Flags [P.], seq 1:396, ack 252, win 122, options [nop,nop,TS val 28276945 ecr 15969203], length 395
E....P@.@.d .......u.P.+.......q...zQ......
..x.....HTTP/1.1 200 OK
Date: Fri, 15 Feb 2013 06:40:01 GMT
Set-Cookie: JSESSIONID=AA93F68D2C44CDCAB57554AB70B91058; Path=/
Content-Type: text/html;charset=utf8
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 112
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive

.............)I-........I.-*J.+.s+.TpKMR04."+..+.C.....#.Cc.}.*.....R.v......}..(....Z\....j.`.......[.....'}...
15:40:01.745860 IP 192.168.3.117.48427 > 192.168.137.2.80: Flags [.], ack 396, win 245, options [nop,nop,TS val 15969203 ecr 28276945], length 0
E..4..@.?.r3...u.....+.P...q...)....?8.....
......x.
15:40:01.765863 IP 192.168.3.117.48427 > 192.168.137.2.80: Flags [F.], seq 252, ack 396, win 245, options [nop,nop,TS val 15969207 ecr 28276945], length 0
E..4..@.?.r2...u.....+.P...q...)....?3.....
......x.
15:40:01.765939 IP 192.168.137.2.80 > 192.168.3.117.48427: Flags [F.], seq 396, ack 253, win 122, options [nop,nop,TS val 28276950 ecr 15969207], length 0
......@.@.e........u.P.+...)...r...z
..x.....
15:40:01.768316 IP 192.168.3.117.48427 > 192.168.137.2.80: Flags [.], ack 397, win 245, options [nop,nop,TS val 15969208 ecr 28276950], length 0
E..4..@.?.r1...u.....+.P...r...*....?,.....
......x.
15:40:01.773846 IP 192.168.3.117.40642 > 192.168.137.2.80: Flags [S], seq 343617337, win 14600, options [mss 1460,sackOK,TS val 15969209 ecr 0,nop,wscale 6], length 0
E..<..@.?..s...u.......P.{/9......9.q..........
............
15:40:01.773855 IP 192.168.137.2.80 > 192.168.3.117.40642: Flags [S.], seq 2163692151, ack 343617338, win 14480, options [mss 1460,sackOK,TS val 28276952 ecr 15969209,nop,wscale 7], length 0
..........,........u.P....Rw.{/:..8.
..x.........
15:40:01.775312 IP 192.168.3.117.40642 > 192.168.137.2.80: Flags [.], ack 1, win 229, options [nop,nop,TS val 15969209 ecr 28276952], length 0
E..4..@.?..z...u.......P.{/:..Rx...........
......x.
15:40:01.775952 IP 192.168.3.117.40642 > 192.168.137.2.80: Flags [P.], seq 1:305, ack 1, win 229, options [nop,nop,TS val 15969209 ecr 28276952], length 304
E..d..@.?..I...u.......P.{/:..Rx....}......
......x.GET /test.jsp HTTP/1.1
Content-type: application/x-www-form-urlencoded;charset=utf8
Connection: keep-alive
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.0.4; SHV-E210S Build/IMM76D)
Host: 192.168.137.2
Accept-Encoding: gzip
Cookie: JSESSIONID=AA93F68D2C44CDCAB57554AB70B91058
Content-Length: 0
15:40:01.775977 IP 192.168.137.2.80 > 192.168.3.117.40642: Flags [.], ack 305, win 122, options [nop,nop,TS val 28276953 ecr 15969209], length 0
......@.@..[.......u.P....Rx.{0j...z
..x.....
15:40:01.776954 IP 192.168.137.2.80 > 192.168.3.117.40642: Flags [P.], seq 1:332, ack 305, win 122, options [nop,nop,TS val 28276953 ecr 15969209], length 331
E...*.@.@..........u.P....Rx.{0j...zlq.....
..x.....HTTP/1.1 200 OK
Date: Fri, 15 Feb 2013 06:40:01 GMT
Content-Type: text/html;charset=utf8
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 113
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive

.............)I-........I.-*J.+.s+.TpKMR04."+..+.C.....#.Cc.}.*.....R.v......}..(....Z\....j..`...........~..~...
15:40:01.778945 IP 192.168.3.117.40642 > 192.168.137.2.80: Flags [.], ack 332, win 245, options [nop,nop,TS val 15969210 ecr 28276953], length 0
E..4..@.?..x...u.......P.{0j..S............
......x.
15:40:01.781966 IP 192.168.3.117.40642 > 192.168.137.2.80: Flags [F.], seq 305, ack 332, win 245, options [nop,nop,TS val 15969210 ecr 28276953], length 0
E..4..@.?..w...u.......P.{0j..S............
......x.
15:40:01.782049 IP 192.168.137.2.80 > 192.168.3.117.40642: Flags [F.], seq 332, ack 306, win 122, options [nop,nop,TS val 28276955 ecr 15969210], length 0
......@.@..Y.......u.P....S..{0k...z
..x.....
15:40:01.783749 IP 192.168.3.117.40642 > 192.168.137.2.80: Flags [.], ack 333, win 245, options [nop,nop,TS val 15969211 ecr 28276955], length 0
E..4..@.?..v...u.......P.{0k..S............
......x.

在第一次连接中,TCP 端口 48427 用于连接服务器。下一次,我希望重新使用 TCP 套接字,但是,使用 TCP 端口 40642 新创建的 TCP 连接。有什么我想念重新使用套接字吗?

添加:我在 tcpdump 日志中发现了一些意外行为。 Web 服务器在 html header 中发送带有“Keep-Alive:timeout=5,max=100”和“Connection:Keep-Alive”选项的 html 页面。但是,设备立即发送 FIN,无需等待 5 秒。我怀疑设备可能无法解析保持 Activity 或超时选项。我在装有 Android 4.0.4 的 Galaxy S3 中测试了这段代码。

最佳答案

看起来这是 Android 中的一个错误。 Keep-Alive 不适用于 gzipped 内容。

https://code.google.com/p/android/issues/detail?id=43132

关于java - 在 android 中使用 HttpURLConnection 重用套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14889825/

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