You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android模拟器无法访问本地NancyFx Mock服务器问题求助

Fixing "This site can't be reached" When Accessing Local NancyFx Mock from Emulator via Charles

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 ipconfig in Command Prompt, look for "IPv4 Address" under your active network adapter (e.g., 192.168.1.105).
    • Mac/Linux: Run ifconfig or ip addr show in Terminal, find the IP under your Wi-Fi/Ethernet interface.
  • In the emulator, visit http://[your-laptop-lan-ip]:9000/test instead of 127.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

  1. Open Charles, go to Proxy > Proxy Settings.
  2. Switch to the Proxies tab.
  3. Check the box labeled Allow remote computers to connect.
  4. 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:
    1. Open Windows Defender Firewall > Advanced Settings > Inbound Rules > New Rule.
    2. Select "Port" > Next > Choose "TCP" and enter 9000 as the specific local port.
    3. Select "Allow the connection" > Next > Check all network profiles (Domain, Private, Public) if testing, then name the rule and finish.
  • Mac:
    1. Go to System Settings > Network > Firewall > Options.
    2. 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:

  1. Start your NancyFx server (bound to 0.0.0.0:9000).
  2. Ensure Charles is running with remote connections enabled.
  3. Confirm the emulator's proxy is set to your laptop's LAN IP and port 8888.
  4. Visit http://[your-laptop-lan-ip]:9000/test in the emulator.
  5. Check Charles' session list—you should see the request come through, and the emulator should display "Hello world".

内容的提问来源于stack exchange,提问作者user2879680

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:21:11