HttpGet 返回 UnknownHostException

发布于 2025-01-07 15:04:29 字数 604 浏览 0 评论 0原文

我在 Android 应用程序中使用 WCF REST 服务时遇到问题。 客户端不断抛出UnknownHostException。

HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet("http://windows");
ResponseHandler<String> handler = new BasicResponseHandler();
String result = httpclient.execute(request, handler);

WCF 服务托管在 IISExpress (http://windows:80) 上,它可以在浏览器本地以及同一 LAN 中的远程框中运行(我正在其中开发 ANDROID 客户端)。

Android 权限在清单中正常,我在 google.com 上尝试过 HttpGet,它工作正常。

有人可以解释一下,为什么 android 无法通过 IISExpress 打开与 LAN 中远程计算机上托管的 (http://windows:80) 的连接,而浏览器可以毫无问题地打开它(在两台计算机上)?

谢谢。

I have problem consuming a WCF REST service in my android application.
The client keeps throwing UnknownHostException.

HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet("http://windows");
ResponseHandler<String> handler = new BasicResponseHandler();
String result = httpclient.execute(request, handler);

WCF service is hosted on IISExpress (http://windows:80) and it works localy in the browser as well as in remote box in same LAN (where I am developing ANDROID client).

Android permissions are OK in the manifest and I have tried HttpGet on google.com and it works OK.

Can some one please explain, why android cant open connection to (http://windows:80) thats hosted on remote computer in LAN over IISExpress, while the browser opens it (on both computers) with no problem?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻风 2025-01-14 15:04:29

如果您使用的是 Windows,您的桌面很可能可以访问 http://windows,因为它有一个默认域后缀/搜索范围返回完全限定的域名“windows.xx”,其中 xx = 您的内部域。或者,它可能使用 WINS 解析主机。

android 或 android 模拟器可能没有相同的 DNS 搜索范围/后缀,因此您必须提供 FQDN(完全限定域名,即 windows.yourdomain.com)。

使用“nslookup”(Windows)或“dig”/“host”(Linux)等 DNS 查询工具来验证 Android 或 Android 模拟器尝试使用的 DNS 服务器:

Windows 示例

使用“ipconfig /all”查找您的 DNS 服务器和 DNS 域后缀...

c:\> ipconfig /all

Windows IP Configuration

Host Name . . . . . . . . . . . . : MYLAPTOP
Primary Dns Suffix  . . . . . . . : mydomain.com <<<<
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : mydomain.com <<<<

Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix  . : mydomain.com
Description . . . . . . . . . . . : Intel(R) 82577LM Gigabit Network Connection
Physical Address. . . . . . . . . : xx-xx-xx-xx-xx-xx
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IPv4 Address. . . . . . . . . . . : 192.168.42.101 (Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Wednesday, February 29, 2012 9:28:23 AM
Lease Expires . . . . . . . . . . : Thursday, March 08, 2012 9:28:22 AM   
Default Gateway . . . . . . . . . : 192.168.42.1
DHCP Server . . . . . . . . . . . : 192.168.42.5
DNS Servers . . . . . . . . . . . : 192.168.42.6  <<<<
                                    192.168.42.7
Primary WINS Server . . . . . . . : 192.168.42.6
Secondary WINS Server . . . . . . : 192.168.42.7
NetBIOS over Tcpip. . . . . . . . : Enabled ...

从上面,我们可以看到我们的 DNS 域是“mydomain.com”,我们的主 DNS 服务器是“192.168.42.6”。 DNS 服务器应该能够解析“windows.mydomain.com”(或者在这种情况下您的域是什么)。

验证您的服务器可以解析完全限定的主机名:(

c:\> nslookup windows.mydomain.com 192.168.42.6

将 192.168.42.6 替换为您的 DNS 服务器)

您应该得到类似以下内容(假设在我的情况下 Windows 已向 DHCP 和 DNS 服务器注册为 192.168.42.106:

C:\>nslookup windows.mydomain.com 192.168.42.6
Server:   dns01.mydomain.com
Address:  192.168.42.6
Name:     windows.mydomain.com
Address:  192.168.42.106

如果您得到无效响应,例如...

*** dns01.mydomain.com 找不到 windows: 不存在的域

然后是您正在查找的主机for 不在您提供的 DNS 域中,因此您的 android/模拟器的默认 DNS 域和 DHCP 之间可能不匹配。

或者,您可以直接使用 IP 地址,如上所述。

If you're on Windows, most likely your desktop can access http://windows because it has a default domain postfix / search scope which is returning the fully qualified domain name "windows.x.x", where x.x = your internal domain. Alternatively it might be resolving the host using WINS.

The android or android emulator likely doesn't have the same DNS search scope / postfix, so you'd have to supply the FQDN (fully qualified domain name, i.e. windows.yourdomain.com).

Use a DNS query tool like "nslookup" (windows) or "dig" / "host" (linux) to verify that the DNS server the android or android emulator is trying to use:

Example for Windows

Use "ipconfig /all" to find your DNS server and DNS domain postfix...

c:\> ipconfig /all

Windows IP Configuration

Host Name . . . . . . . . . . . . : MYLAPTOP
Primary Dns Suffix  . . . . . . . : mydomain.com <<<<
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : mydomain.com <<<<

Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix  . : mydomain.com
Description . . . . . . . . . . . : Intel(R) 82577LM Gigabit Network Connection
Physical Address. . . . . . . . . : xx-xx-xx-xx-xx-xx
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IPv4 Address. . . . . . . . . . . : 192.168.42.101 (Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Wednesday, February 29, 2012 9:28:23 AM
Lease Expires . . . . . . . . . . : Thursday, March 08, 2012 9:28:22 AM   
Default Gateway . . . . . . . . . : 192.168.42.1
DHCP Server . . . . . . . . . . . : 192.168.42.5
DNS Servers . . . . . . . . . . . : 192.168.42.6  <<<<
                                    192.168.42.7
Primary WINS Server . . . . . . . : 192.168.42.6
Secondary WINS Server . . . . . . : 192.168.42.7
NetBIOS over Tcpip. . . . . . . . : Enabled ...

From above, we can see that our DNS domain is "mydomain.com" and our primary DNS server is "192.168.42.6". The DNS server should be able to resolve "windows.mydomain.com" (or whatever your domain is in this case).

Verify your server can resolve the fully qualified hostname:

c:\> nslookup windows.mydomain.com 192.168.42.6

(replace 192.168.42.6 with your DNS server)

You should get something like this (assuming in my case that windows registered with the DHCP and DNS server(s) as 192.168.42.106:

C:\>nslookup windows.mydomain.com 192.168.42.6
Server:   dns01.mydomain.com
Address:  192.168.42.6
Name:     windows.mydomain.com
Address:  192.168.42.106

If you get an invalid response like...

*** dns01.mydomain.com can't find windows: Non-existent domain

Then the host you're looking for isn't in the DNS domain that you supplied, so there might be a mismatch between default DNS domain and DHCP for your android / emulator.

Alternatively you could just use the IP address directly, as mentioned above.

旧人 2025-01-14 15:04:29

您是否尝试仅用 IP 替换“windows”主机名?

Did you try just replacing "windows" hostname by the IP?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文