java.net.ConnectException: Connect to XXXhost:XXXport failed: 拒絕連接 (Connection refused)


公司內部的優付系統通過http調用外部服務商系統的API。上周五夜間,服務端系統發版期間,客戶端在通過http調用服務端接口時出現一堆 “拒絕連接 (Connection refused) ” 異常。

2022-04-08 22:22:28,676 ERROR [2022040822222771523885] ? (com.cn.yft.pay.util.HttpClientHelper:) - 請求urlParam 異常:
org.apache.http.conn.HttpHostConnectException: Connect to 123.56.223.21:8280 [/123.56.223.21] failed: 拒絕連接 (Connection refused)
        at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.client.InternalHttpClient.doExecute$original$szvaDZwu(InternalHttpClient.java:184) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.client.InternalHttpClient.doExecute$original$szvaDZwu$accessor$2wpTSoQJ(InternalHttpClient.java) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.client.InternalHttpClient$auxiliary$f5vLomHg.call(Unknown Source) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86) ~[skywalking-agent.jar:8.6.0]
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) ~[httpclient-4.5.2.jar:4.5.2]
        at com.cn.yft.pay.util.HttpClientHelper.httpClientPost(HttpClientHelper.java:506) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
        at com.cn.yft.bank.p600001.own.PaymentTransByOWN.doBusiness$original$a5alvy8U(PaymentTransByOWN.java:150) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
        at com.cn.yft.bank.p600001.own.PaymentTransByOWN.doBusiness$original$a5alvy8U$accessor$RlW8FGim(PaymentTransByOWN.java) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
        at com.cn.yft.bank.p600001.own.PaymentTransByOWN$auxiliary$GTU0SZKs.call(Unknown Source) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
        at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86) [skywalking-agent.jar:8.6.0]
        at com.cn.yft.bank.p600001.own.PaymentTransByOWN.doBusiness(PaymentTransByOWN.java) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
        at com.cn.yft.pay.service.PCPaymentRunnable.run(PCPaymentRunnable.java:50) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_191]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_191]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_191]
Caused by: java.net.ConnectException: 拒絕連接 (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:1.8.0_191]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_191]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_191]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_191]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_191]
        at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_191]
        at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74) ~[httpclient-4.5.2.jar:4.5.2]
        at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141) ~[httpclient-4.5.2.jar:4.5.2]
        ... 23 more

 

參考csdn上網友的Connection refused: connect問題解決方案集合,整理一下http調用出現connection refused異常的各種情況:

 

客戶端:

  • 寫錯了主機和端口
  • 通信協議不正確,http://,https://,rmi://
  • 客戶端服務不在網絡中。它可能沒有連接到LAN或互聯網或任何其他網絡。
  • 客戶端網絡防火牆攔截請求

服務端:

  • 服務掛掉了 / 服務未運行
  • 服務在重啟中 / 服務處於kill -15過程中
  • 服務端網絡防火牆攔截請求
  • 服務端服務不在網絡中。它可能沒有連接到LAN或互聯網或任何其他網絡。
  • 服務器正在運行但沒有偵聽端口,或者服務器正在運行但是正在偵聽不同的端口。

 

網站掛掉,網頁無法訪問,如下這個頁面大家應該熟知。可以注意到code是ERR_CONNECTION_REFUSED

 

 

再簡單測試一下上面的幾種情況。結論是:上面總結的幾種情況並不都返回connection refused異常。至少如下幾種不是。

我司公司網絡限制訪問淘寶、優酷等視頻購物網站。就拿訪問淘寶網(https://www.taobao.com)來說, 我們來 curl https://www.taobao.com 或 curl http://www.taobao.com

C:\Users\zhangguozhan>curl https://www.taobao.com
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - 給函數提供的標志無效

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /
><title>Forbidden</title>
</head>
<body>
你被禁止訪問這個網站,請聯系網站管理員!
</body>
</html>

 

 

把網絡切換成自己手機的4G,再 curl https://www.taobao.com/,  此時可以正常返回響應結果。

 

然后,自己手機4G情況下,把上面的https換成http, 即, 此時,返回的是我們熟知的301(重定向)錯誤。 

C:\Users\zhangguozhan>curl http://www.taobao.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<h1>301 Moved Permanently</h1>
<p>The requested resource has been assigned a new permanent URI.</p>
<hr/>Powered by Tengine</body>
</html>

 

以上,做個記錄,方便日后工作中查證。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM