HttpURLConnection 在数据连接模式下引发异常
我正在尝试从我的应用程序访问远程服务器。这是我正在使用的代码:
URL url = new URL("http", myURL, 80, "");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestProperty("Connection", "close");
urlConnection.setConnectTimeout(1000 * 5);
urlConnection.connect();
使用相同的 myURL
(www.google.com) 变量,当手机处于 wi-fi 状态时,urlConnection.getResponseCode()
返回 200模式,并在数据连接模式下抛出 UnknownHost 异常
。有什么建议吗?这是因为我使用的是80端口吗?
I'm trying to access remote server from my app. This is the code I am using:
URL url = new URL("http", myURL, 80, "");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestProperty("Connection", "close");
urlConnection.setConnectTimeout(1000 * 5);
urlConnection.connect();
With the same myURL
(www.google.com) variable, urlConnection.getResponseCode()
returns 200 when the phone is in wi-fi mode and throws an UnknownHost exception
when in data connection mode. Any suggestions? Is this because I'm using port 80?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您的远程服务器无法通过其主机名解析时,
UnknownHost 异常
会抛出,请尝试使用服务器 IP 连接到它(如果主机允许)并检查您的 DNS 并确保您没有在代理后面。The
UnknownHost exception
is thrown ~mostly~ when your remote server cannot be resolved by its hostname, try connecting to it with server IP (if the host allows that) and check your DNS and make sure you are not behind proxy.与端口80无关。
我在连接Wi-Fi和数据服务时遇到了同样的问题。使用 wi-fi 时我能够连接到部署 Web 服务的服务器,但使用数据服务时却出现异常。为了处理这个问题,我使用公共 IP 从网络外部进行访问。在您拥有私有 IP 之前,您将无法从网络外部的任何系统或电话访问它。
It has nothing to do with port 80.
I had the same issue while connecting with wi-fi and data service. I was able to connect to the server where my web service was deployed when using wi-fi but when using data services it gave me exception. To handle this i used the public ip to get access from outside of my network. Till the time you have a private ip you won't be able to access it from any sysytem or phone's outside your network.