HttpGet返回UnknownHostException

时间:2012-02-20 11:25:25

标签: android wcf http-get

我在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)上,它可以在浏览器中以及同一局域网(我正在开发的ANDROID客户端)的远程盒中工作。

清单中的Android权限正常,我在google.com上尝试了HttpGet,它运行正常。

有人可以解释一下,为什么android无法通过IISExpress打开连接到局域网远程计算机上的(http:// windows:80),而浏览器打开它(在两台计算机上)没有问题?

谢谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

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

使用DNS查询工具,如“nslookup”(windows)或“dig”/“host”(linux)来验证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

(用您的DNS服务器替换192.168.42.6)

你应该得到这样的东西(假设在我的情况下,用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 can't find windows: Non-existent domain

然后您正在寻找的主机不在您提供的DNS域中,因此您的Android /仿真器的默认DNS域和DHCP之间可能存在不匹配。

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

相关问题