1

I am trying to get a token for an API with this code and it throws exception when it tries to get and check the response code. It is my understanding that url.openConnection() does not actually connect? I tried url.connect() but that didn't work for me. When I

System.out.println(conn);

i just get this:

sun.net.www.protocol.https.DelegateHttpsURLConnection:https://fim.api.ENV.fleetmatics.com/token

and when I

System.out.println(conn.getResponseCode());

I do not get anything.

public TokenGenerator(String username, String password, String getTokenUri){
      _username = username;
      _password = password;
      _getTokenUri = getTokenUri;
      _authString = GetToken();
    }

    private String GetToken(){
      String authString = "";
      try
      {         
        // Get authentication, Base64 encoded, string     
        String basicAuth = BuildBasicAuth(_username, _password);

        URL url = new URL(_getTokenUri);

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();   
        conn.setRequestMethod("GET");                                          
        conn.setRequestProperty("Authorization", basicAuth);                   
        conn.setRequestProperty("Accept", "text/plain");                  
        conn.setDoOutput(false);                                               

        // Execute request;, read response. If we do not receive a 200 OK, throw exception      
        if (conn.getResponseCode() != HttpsURLConnection.HTTP_OK) {
          throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

When I try to connect with this code, I get the stack trace below..

HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();  
    conn.connect();
    conn.setRequestMethod("GET");                                          
    conn.setRequestProperty("Authorization", basicAuth);                   
    conn.setRequestProperty("Accept", "text/plain");                       
    conn.setDoOutput(false);  





java.net.UnknownHostException: fim.api.ENV.fleetmatics.com
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
    at TokenGenerator.GetToken(TokenGenerator.java:52)
    at TokenGenerator.<init>(TokenGenerator.java:39)
    at Program.main(Program.java:10)
Exception in thread "main" java.lang.NullPointerException
    at java.util.Date.getMillisOf(Unknown Source)
    at java.util.Date.after(Unknown Source)
    at TokenGenerator.GetAuthString(TokenGenerator.java:28)
    at Program.main(Program.java:13)
paul
  • 135
  • 1
  • 10
  • Please provide a [minimal, complete and verifiable](https://stackoverflow.com/help/mcve) example of what you are trying. That will greatly improve the possibility of constructive answers. – M. le Rutte Oct 05 '17 at 14:38
  • Sorry I forgot to insert the code, edited. – paul Oct 05 '17 at 14:55
  • Are you sure the hostname is ok? I can't ping to that address, yet I can ping to `fim.api.env.fleematics.com` – M. le Rutte Oct 05 '17 at 15:14
  • see I cannot even ping to `fim.api.env.fleematics.com` – paul Oct 05 '17 at 15:21
  • Okay I am guessing this may be a firewall issue on my end, maybe it is blocking me from reaching out. I will update if I get to the bottom, thank you. – paul Oct 05 '17 at 15:32
  • Resolved, turns out it was an incorrect link third party provided. – paul Oct 05 '17 at 19:17

0 Answers0