使用HttpsURLConnection与Selenium无法读取香港交易所指定行情页面的问题求助
HttpsURLConnection与Selenium无法读取香港交易所指定行情页面的问题求助
大家好,最近我碰到了个特别棘手的问题:我想用Java读取香港交易所的行情页面(比如https://www.hkex.com.hk/eng/stat/smstat/dayquot/d250602e.htm),但不管怎么尝试都会触发Read timed out错误,想请各位帮忙分析下可能的原因,给点排查思路。
我一开始用HttpsURLConnection写了基础请求代码,如下:
try { URL url = new URL("https://www.hkex.com.hk/eng/stat/smstat/dayquot/d250620e.htm"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); } catch (Exception e) { e.printStackTrace(); }
我试过把读取超时调到240秒(conn.setReadTimeout(240000)),但还是会超时。其他HTTPS页面用这段代码都能正常读取,只有港交所的这类行情页面不行。
后来我用curl和PowerShell的Invoke-WebRequest测试,也完全收不到响应,只有在浏览器里打开页面是正常的。
有朋友建议我用Selenium的getSource方法,但执行后提示页面已关闭或转移,可我在Chrome里手动查看页面源码是完全没问题的。
之后我又尝试用Selenium的DevTools监听网络请求,获取响应体,代码如下:
devTools.addListener(Network.responseReceived(), responseReceived -> { String responseUrl = responseReceived.getResponse().getUrl(); RequestId requestId = responseReceived.getRequestId(); if (responseUrl.contains("smstat/dayquot")) { System.out.println("Url: " + responseUrl); System.out.println("Response headers: " + responseReceived.getResponse().getHeaders().toString()); // 注:原代码此处有截断,保留原样 25 -> System.out.println("Response body: " + devTools.send(Network.getResponseBody(requestId)).getBody()); } });
运行这段代码时,Chrome弹窗确实能加载出我想要的内容,但程序会抛出警告并终止,错误日志如下:
Jul 03, 2025 8:20:24 AM org.openqa.selenium.devtools.Connection$Listener lambda$onText$0 WARNING: Unable to process: {"method":"Network.responseReceived","params":{"requestId":"D32E463F42A6CAC6BE6A7E39FA5C1023","loaderId":"D32E463F42A6CAC6BE6A7E39FA5C1023","timestamp":4344.313103,"type":"Document","response":{"url":"https://www.hkex.com.hk/eng/stat/smstat/dayquot/d250618e.htm","status":200,"statusText":"","headers":{"accept-ranges":"bytes","content-encoding":"gzip","content-type":"text/html","date":"Thu, 03 Jul 2025 00:20:23 GMT","etag":"\"07ab78648e0db1:0\"","last-modified":"Wed, 18 Jun 2025 12:00:04 GMT","strict-transport-security":"max-age=480","vary":"Accept-Encoding","x-akamai-transformed":"0 - 0 -"},"mimeType":"text/html","charset":"","connectionReused":false,"connectionId":52,"remoteIPAddress":"23.35.148.143","remotePort":443,"fromDiskCache":false,"fromServiceWorker":false,"fromPrefetchCache":false,"encodedDataLength":1281,"timing":{"requestTime":4342.845654,"proxyStart":-1,"proxyEnd":-1,"dnsStart":-1,"dnsEnd":-1,"connectStart":-1,"connectEnd":-1,"sslStart":-1,"sslEnd":-1,"workerStart":-1,"workerReady":-1,"workerFetchStart":-1,"workerRespondWithSettled":-1,"sendStart":1110.392,"sendEnd":1110.758,"pushStart":0,"pushEnd":0,"receiveHeadersStart":1462.685,"receiveHeadersEnd":1463.48},"responseTime":1.751502024562685e+12,"protocol":"h2","alternateProtocolUsage":"unspecifiedReason","securityState":"secure","securityDetails":{"protocol":"TLS 1.3","keyExchange":"","keyExchangeGroup":"X25519","cipher":"AES_256_GCM","certificateId":0,"subjectName":"*.hkex.com.hk","sanList":["*.hkex.com.hk","hkex.com.hk"],"issuer":"Sectigo RSA Organization Validation Secure Server CA","validFrom":1728432000,"validTo":1761609599,"signedCertificateTimestampList":[{"status":"Verified","origin":"Embedded in certificate","logDescription":"Google 'Xenon2025h2' log","logId":"DDDCCA3495D7E11605E79532FAC79FF83D1C50DFDB003A1412760A2CACBBC82A","timestamp":1728445455641,"hashAlgorithm":"SHA-256","signatureAlgorithm":"ECDSA","signatureData":"3044022042FB38EF079A0BE8355ABEA16524CCE9FB1F4433806E00D735D097683638B512022015F93705DECE739B4EC30CCDEA42F9791B86F1B4D"}}}
现在我实在没辙了,有没有朋友遇到过类似的情况?或者能给我一些排查的方向?非常感谢大家!
内容来源于stack exchange
相关产品推荐
相关产品推荐

