https://kotlinlang.org logo
#ktor
Title
# ktor
p

phil

09/21/2021, 6:27 PM
👋 Hi folks! I’m running into a weird situation using ktor’s
HttpClient
to send a
POST
request to a host in a KMM project. When running the project from an Android app, if I manually set up the Android device on a network by selecting it in the Android system settings, I’m able to connect to the host and send the request using ktor successfully. However if I connect the Android device to the network programmatically via Android’s requestNetwork API, my request with ktor fails immediately with the error
java.net.ConnectException: Failed to connect to /<host_ip_address>:80
. Has anyone ever encountered this before, or have any idea as to what might be going on?
Here’s the full stack trace. It suggests the address is unreachable, but I’m able to ping it successfully 🤔
Copy code
java.net.ConnectException: Failed to connect to /192.168.10.1:80
        at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:297)
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:207)
        at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
        at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
        at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
        at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
        at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)
     Caused by: java.net.ConnectException: failed to connect to /192.168.10.1 (port 80) from /:: (port 0) after 10000ms: connect failed: ENETUNREACH (Network is unreachable)
        at libcore.io.IoBridge.connect(IoBridge.java:142)
        at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
        at java.net.Socket.connect(Socket.java:621)
        at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:120)
        at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295)
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:207) 
        at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226) 
        at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106) 
        at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74) 
        at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255) 
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
        at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) 
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:923) 
     Caused by: android.system.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
        at libcore.io.Linux.connect(Native Method)
        at libcore.io.ForwardingOs.connect(ForwardingOs.java:94)
        at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:138)
        at libcore.io.ForwardingOs.connect(ForwardingOs.java:94)
        at libcore.io.IoBridge.connectErrno(IoBridge.java:173)
        at libcore.io.IoBridge.connect(IoBridge.java:134)
        at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142) 
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390) 
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230) 
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212) 
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436) 
        at java.net.Socket.connect(Socket.java:621) 
        at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:120) 
        at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295) 
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:207) 
        at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226) 
        at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106) 
        at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74) 
        at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255) 
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
        at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) 
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
9 Views