2016-05-09 40 views
2

我想爲項目使用來自https://us.mc-api.net/的API,並且我已將其作爲測試。在瀏覽器中工作的URL的FileNotFoundException

public static void main(String[] args){ 
    try { 
     URL url = new URL("http://us.mc-api.net/v3/uuid/193nonaxishsl/csv/"); 
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
      String line; 
      while ((line = in.readLine()) != null) { 
       System.out.println(line); 
       } 
      in.close(); 
       } 
        catch (MalformedURLException e) { 
         e.printStackTrace(); 
        } 
        catch (IOException e) { 
         e.printStackTrace(); 
         System.out.println("I/O Error"); 

        } 
       } 
} 

,這是給我一個IOException錯誤,但是當過我在我的web瀏覽器中打開同一個網頁,我得到

false,Unknown-Username 

這就是我想從代碼即可獲得。我是新人,不知道爲什麼會發生這種情況,或者爲什麼。 編輯:堆棧跟蹤

java.io.FileNotFoundException: http://us.mc-api.net/v3/uuid/193nonaxishsl/csv/ 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 
at java.net.URL.openStream(Unknown Source) 
at com.theman1928.Test.Main.main(Main.java:13) 
+0

我實際上認爲這會起作用,追蹤它我可以看到行應該有「false,未知的用戶名」。你可以發佈拋出IOException時給出的堆棧跟蹤嗎? –

+0

儘管將內容返回給瀏覽器,但給定的URL將返回404 Not Found HTTP錯誤。如果這個Web服務是你的,那麼讓它返回一個200 OK,你會沒事的。 – ManoDestra

+0

@ManoDestra這不是我的服務,但如果它更棘手的問題,我會嘗試聯繫他們或找到我可以使用的不同服務。 – theman1928

回答

3

的URL返回狀態碼404,並因此沒有被創建的輸入流(輕度猜測這裏),因此爲空。對狀態碼進行排序,您應該可以。

使用CSV運行它,它是罰款:other csv

如果錯誤代碼是對你很重要,那麼你可以使用HttpURLConnection類:

HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    System.out.println("code:"+conn.getResponseCode()); 

這樣,你就可以前處理響應代碼繼續進行快速的if-then-else檢查。

1

這與線程協議如何與java.net類和實際瀏覽器相比起作用。瀏覽器將比你使用的簡單的java.net API複雜得多。

如果您想在Java中獲得等效的響應值,那麼您需要使用更豐富的HTTP API。

此代碼會給你與瀏覽器相同的響應;但是,你需要下載Apache HttpComponents罐子

代碼:

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URL; 
import javax.net.ssl.HttpsURLConnection; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpUriRequest; 
import org.apache.http.impl.client.HttpClients; 

public class TestDriver 
{ 

public static void main(String[] args) 
{ 
    try 
    { 
     String url = "http://us.mc-api.net/v3/uuid/193nonaxishsl/csv"; 

     HttpGet httpGet = new HttpGet(url); 
     getResponseFromHTTPReq(httpGet, url); 
    } 
    catch (Throwable e) 
    { 
     e.printStackTrace(); 
    } 
} 

private static String getResponseFromHTTPReq(HttpUriRequest httpReq, String url) 
{ 
    HttpClient httpclient = HttpClients.createDefault(); 

    // Execute and get the response. 
    HttpResponse response = null; 
    HttpEntity entity = null; 
    try 
    { 
     response = httpclient.execute(httpReq); 
     entity = response.getEntity(); 
    } 
    catch (IOException ioe) 
    { 
     throw new RuntimeException(ioe); 
    } 

    if (entity == null) 
    { 
     String errMsg = "No response entity back from " + url; 
     throw new RuntimeException(errMsg); 
    } 

    String returnRes = null; 
    InputStream is = null; 
    BufferedReader buf = null; 
    try 
    { 
     is = entity.getContent(); 
     buf = new BufferedReader(new InputStreamReader(is, "UTF-8")); 

     System.out.println("Response Code : " + response.getStatusLine().getStatusCode()); 

     StringBuilder sb = new StringBuilder(); 
     String s = null; 
     while (true) 
     { 
      s = buf.readLine(); 
      if (s == null || s.length() == 0) 
      { 
       break; 
      } 
      sb.append(s); 
     } 

     returnRes = sb.toString(); 

     System.out.println("Response: [" + returnRes + "]"); 
    } 
    catch (UnsupportedOperationException | IOException e) 
    { 
     throw new RuntimeException(e); 
    } 
    finally 
    { 
     if (buf != null) 
     { 
      try 
      { 
       buf.close(); 
      } 
      catch (IOException e) 
      { 
      } 
     } 
     if (is != null) 
     { 
      try 
      { 
       is.close(); 
      } 
      catch (IOException e) 
      { 
      } 
     } 
    } 
    return returnRes; 
} 

} 

輸出

響應代碼:404

迴應:假的,未知的,用戶名]

1

我用Apache HTTP庫試了一下。 API端點似乎返回404的狀態碼,因此您的錯誤。我使用的代碼如下。

public static void main(String[] args) throws URISyntaxException, ClientProtocolException, IOException { 
    HttpClient httpclient = HttpClients.createDefault(); 
    URIBuilder builder = new URIBuilder("http://us.mc-api.net/v3/uuid/193nonaxishsl/csv/"); 
    URI uri = builder.build(); 
    HttpGet request = new HttpGet(uri); 
    HttpResponse response = httpclient.execute(request); 
    System.out.println(response.getStatusLine().getStatusCode()); // 404 
} 

切換出與http://us.mc-api.net/v3/uuid/193nonaxishsl/csv/www.example.com任何返回的狀態代碼200,這進一步證明了錯誤使用API​​端點。你可以看看[Apache HTTP Components] library here

相關問題