HttpGet 返回 UnknownHostException
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 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 域后缀...
从上面,我们可以看到我们的 DNS 域是“mydomain.com”,我们的主 DNS 服务器是“192.168.42.6”。 DNS 服务器应该能够解析“windows.mydomain.com”(或者在这种情况下您的域是什么)。
验证您的服务器可以解析完全限定的主机名:(
将 192.168.42.6 替换为您的 DNS 服务器)
您应该得到类似以下内容(假设在我的情况下 Windows 已向 DHCP 和 DNS 服务器注册为 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...
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:
(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:
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.
您是否尝试仅用 IP 替换“windows”主机名?
Did you try just replacing "windows" hostname by the IP?