Java HttpsURLConnection实例没有给出响应代码

时间:2017-10-05 14:36:55

标签: java httpsurlconnection getresponse

我正在尝试使用此代码获取API的令牌,并在尝试获取并检查响应代码时抛出异常。我的理解是url.openConnection()实际上没有连接?我试过url.connect(),但这对我没有用。当我

System.out.println(conn);

我得到这个:

  

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

当我

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

我什么都没得到。

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());
        }

当我尝试连接此代码时,我得到下面的堆栈跟踪..

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)

0 个答案:

没有答案