2012-02-20 109 views
0

我有我的Android应用程序中使用WCF REST服务的问题。 客户端一直抛出UnknownHostException。HttpGet返回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://窗口:80)和它的作品localy在浏览器中,以及在同一个局域网(这里我开发Android客户端)遥控盒。

Android权限在清单中是可以的,我在Google.com上试过HttpGet,它工作正常。

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

谢谢。

+0

您是否尝试过使用服务器的直接IP而不是计算机名称? – denolk 2012-02-20 11:28:04

+0

那不是一个完整的URL,它没有机器的IP地址。 – jeet 2012-02-20 11:29:42

+0

请检查TeddyBearFr's anwsers的评论 – no9 2012-02-20 11:43:14

回答

1

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

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

使用像“NSLOOKUP”(Windows)或“挖” /“主机”(Linux)的DNS查询工具验证DNS服务器的Android或Android模拟器尝试使用:

示例视窗

使用“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服务器(S)为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域,所以有可能是默认的DNS域和DHCP之间的不匹配将Android /仿真器。

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

1

你试过用IP代替“windows”主机名吗?

+0

我的IP主机托管IIS主页,而WCF服务在http:// hostname:80上的IISExpress上运行。 – no9 2012-02-20 11:40:00

+0

我遵循http://www.hanselman.com/blog/WorkingWithSSLAtDevelopmentTimeIsEasierWithIISExpress.aspx在IISExpress上获得它。我应该放下IIS并将IISExpress设置为在IP上运行?有没有办法使用计算机主机名使用ANDROID应用程序中的服务? – no9 2012-02-20 11:42:41

+0

正如jitendra sharma所说,“windows”不是完整的合格域名。首先尝试FQDN。这是一个DNS问题,而不是我认为的IIS问题。 – Sly 2012-02-20 14:15:34