开发者

HttpURLConnection is causing exception when in data connection mode

开发者 https://www.devze.com 2023-03-27 19:17 出处:网络
I\'m trying to access remote server from my app. This is the code I am using: URL url = new URL(\"http\", myURL, 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"开发者_C百科, "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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消