Android模拟器无法访问本地NancyFx Mock服务器问题求助
Let me break down the issues here and walk you through the fixes step by step—this is a super common gotcha when setting up local mocks with emulators and proxies!
Core Problem #1: Emulator's 127.0.0.1 Isn't Your Laptop
When you type 127.0.0.1:9000/test into the emulator's browser, it's trying to connect to its own local 9000 port, not your laptop's NancyFx server. That's why your laptop's browser works (it's pointing to itself) but the emulator fails.
Fix: Use Your Laptop's Local Network IP
- Find your laptop's LAN IP address:
- Windows: Run
ipconfigin Command Prompt, look for "IPv4 Address" under your active network adapter (e.g.,192.168.1.105). - Mac/Linux: Run
ifconfigorip addr showin Terminal, find the IP under your Wi-Fi/Ethernet interface.
- Windows: Run
- In the emulator, visit
http://[your-laptop-lan-ip]:9000/testinstead of127.0.0.1:9000/test.
Core Problem #2: NancyFx Might Be Bound Only to Localhost
By default, some web servers (including NancyFx if not configured otherwise) only listen on 127.0.0.1, which means they won't accept connections from other devices on the network (like your emulator).
Fix: Bind NancyFx to 0.0.0.0
Modify your NancyFx startup code to bind to 0.0.0.0 instead of 127.0.0.1—this lets it listen for requests from any network interface:
var host = new NancyHost(new Uri("http://0.0.0.0:9000")); host.Start();
This ensures your mock server is accessible to devices on your local network.
Core Problem #3: Charles Might Be Blocking Remote Connections
Charles won't accept proxy requests from external devices (like your emulator) unless you explicitly enable it.
Fix: Allow Remote Connections in Charles
- Open Charles, go to
Proxy > Proxy Settings. - Switch to the
Proxiestab. - Check the box labeled Allow remote computers to connect.
- Restart Charles to apply the setting.
Core Problem #4: Laptop Firewall Blocking Port 9000
Your Windows Defender Firewall (or third-party firewall) is likely blocking incoming connections to port 9000, preventing the emulator from reaching your NancyFx server.
Fix: Add a Firewall Rule for Port 9000
- Windows:
- Open Windows Defender Firewall > Advanced Settings > Inbound Rules > New Rule.
- Select "Port" > Next > Choose "TCP" and enter
9000as the specific local port. - Select "Allow the connection" > Next > Check all network profiles (Domain, Private, Public) if testing, then name the rule and finish.
- Mac:
- Go to System Settings > Network > Firewall > Options.
- Click the "+" button, select your NancyFx application (or add a port rule for TCP 9000), and allow incoming connections.
Verify the Flow
After making these changes:
- Start your NancyFx server (bound to
0.0.0.0:9000). - Ensure Charles is running with remote connections enabled.
- Confirm the emulator's proxy is set to your laptop's LAN IP and port 8888.
- Visit
http://[your-laptop-lan-ip]:9000/testin the emulator. - Check Charles' session list—you should see the request come through, and the emulator should display "Hello world".
内容的提问来源于stack exchange,提问作者user2879680

