"Connection refused: connect" in Http Client when querying localhost. Follow
I try to configure development on Windows with WSL 2.
I've managed to import, compile and run a simple Spring Boot application with a /users endpoint.
I got a response from localhost:8080/users using telnet via Windows command propt:
telnet localhost 8080
Connection to host lost.
I can get a response with a WCL console too, with
$ curl -X GET --location "http://localhost:8080/users"
...but not with Idea's HTTP Client
org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused: connect
- I did run the "Fix Firewall" action in IDE
- There is no error message in Spring Boot log
- I run Spring Boot application on WSL target
- Got the same result when running on "Local Machine" though
- Got the same result after completely disabling Windows Defender Firewall
- I don't have any other firewall software installed.
- I am a simple man and I don't have any unusual network configuration.
IntelliJ IDEA 2022.2 (Ultimate Edition)
Build #IU-222.3345.118, built on July 26, 2022
Licensed to Jacek Gajek
Subscription is active until September 11, 2022.
Runtime version: 17.0.3+7-b469.32 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
YAML/Ansible support (0.11.2)
PlantUML integration (5.16.1)
Base64 for IDEA and Storm (1.1b)
Please sign in to leave a comment.
Does it help if you use IP address instead? (may be related to https://github.com/microsoft/WSL/discussions/6253)
You may also try adding -Djava.net.preferIPv6Addresses=true to the run configuration's VM options:
No, I've already tried both of these options. Sorry for not including that in the original post.
I also have tried -Djava.net.preferIPv4Addresses=false which I suppose does the same thing.
I created a new usability problem report for this on our system: https://youtrack.jetbrains.com/issue/IDEA-299087
We will come up with a way to handle this.
As a workaround, try running the server with server.address=0.0.0.0 (see similar issue with Quarkos: https://github.com/quarkusio/quarkus/issues/22887)
Thanks for support. Unfortunately setting "server.address=0.0.0.0" in application.properties didn't change anything for me.
When I run the app by IDE on Local Machine, the only part related to WSL is the java runtime, which happens to be located on the Linux subsystem, right? Shouldn't be an issue... any other ideas?
Thanks for the info.
I'll ask the developers to look into it. If there's another workaround, will let you know.
For the record, the Http Client works if I launch Idea from WSL console, via VcXsrv. But it's not a workaround, rather a curiosity. WSL 2 (at least on Windows 10) is not suited to run GUI apps for many reasons, even if it's technically possible.
I can access localhost endpoints via PostMan.
This code, when run with Idea's "play" button, also works when targeted Local Machine or WSL.
I'm also having this issue on IntelliJ IDEA 2022.2.2 (Ultimate Edition)
Two IDEA projects are open: IDEA #1 is a Spring project on the WSL 2 filesystem. IDEA #2 is a separate Java project on the Windows filesystem. I'm running project #1 to serve an API, and trying to make a request to the API from an apache HttpClient in a unit test on #2. I can hit the endpoint successfully from the Jetbrains HTTP Client on both #1 and #2 (or from the browser, or postman, etc), but it's showing "Connection Refused: connect" when trying to run the code itself in IDEA #2.
Also note, when I deploy the API to https://some.deployed.location.com the code works just fine. So it seems to specifically be some problem with IDEA #2 on Windows running the code, which is trying to connect to a local Spring instance running on WSL 2.