0

I am trying to access an IP address from my android app but I am not have any luck. Here is my async task that I am trying:

protected String doInBackground(Void...arg0) {
       try{
            URL url = new URL(myIP);
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            String read = readStream(con.getInputStream());
            return read;
        } catch(Exception e){
            e.printStackTrace();
        }

        return null;
    }

    private String readStream(InputStream in){
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder("xxxx");
        try{
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while((line = reader.readLine()) != null){
                sb.append(line + "\n");
            }
        } catch(IOException e){
            e.printStackTrace();
        } finally{
            if(reader != null){
                try{
                    reader.close();
                } catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    protected void onPostExecute(String result){
        textView.setText(result);
    }

When I use a normal url like www.stackoverflow.com it works fine, but when I try to use an IP there is just no response whatsoever. It's not throwing any exceptions and it is not updating the textView(I have tried returning different results based on which exception is thrown). So this brings me to my question, is an IP address a valid input for a URLconnection? Let me know if there is any other information I can provide.

It also may be helpful that my end goal is to send REST requests to this IP to access particular information.

user2483079
  • 533
  • 2
  • 10
  • 1
    Have you checked that the CN on the SSL certificate is the IP address you are trying to access and not a domain name? – DaveTrux Jun 13 '13 at 19:19
  • I'm not entirely sure what you are referring to as I'm fairly new to this. I know that the IP is reachable via a browser and works properly, not sure if that helps answer your question though. – user2483079 Jun 13 '13 at 19:28
  • SSL certificates are created for a specific name. That name can be a normal domain name, like www.stackoverflow.com or an IP address, but not both. Browsers will let you access SSL via IP address or name, but is should complain when you browse to the one that does not match the certificate. In my experience APIs are less forgiving than browsers in this regard. Also, if the SSL certificate is self-signed (you didn't buy it from a Certificate Authority, you made it yourself), then you can have issues as well. – DaveTrux Jun 13 '13 at 20:05
  • Ok, how would I check to answer your original question then? There is no specified domain name, just the IP address. – user2483079 Jun 13 '13 at 20:10
  • In Chrome, click the lock icon in the address bar. There is a connection tab. Inside that tab there is a link for Certificate Information that will display the certificate details. Look in the details of the dialog that opens. Check the common name (CN). – DaveTrux Jun 13 '13 at 20:23
  • It should just read "common name" – DaveTrux Jun 13 '13 at 20:43
  • There was an entry that said Subject and when I clicked on it a couple more details showed up and one was the CN. – user2483079 Jun 13 '13 at 20:44
  • possible duplicate of [HTTPS GET (SSL) with Android and self-signed server certificate](http://stackoverflow.com/questions/3761737/https-get-ssl-with-android-and-self-signed-server-certificate) – AlikElzin-kilaka Aug 28 '14 at 09:04

3 Answers3

1

HttpsUrlConnection class is for HTTPS connections, if you want to access a website using an IP your myIP variable should be something like this:

String myIP="https://62.123.123.123/index.php" //for example

If however, you want to access to a remote machine for direct communication you should use Sockets.

check this out

Hope it helps :)

Community
  • 1
  • 1
zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
  • The particular IP address goes to a web UI of a service. myIP is in that format and it is not working. I will look further into sockets but it seemed much more complicated than what I need. – user2483079 Jun 13 '13 at 17:28
0

I did not have access to the IP because of the network I was on. In short, the answer to my original question is yes, IP addresses are vaild for HttpURLConnections.

user2483079
  • 533
  • 2
  • 10
0

I had this problem and it turned out that you can't put the IP address in the CN field of the certificate, it has to go in the Subject Alternative Name field instead.

lane
  • 633
  • 12
  • 18