filenotfoundexception

lp0sw83n  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(396)

我正在尝试使用来自https://us.mc-api.net/ 为了一个项目,我做了一个测试。

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错误,但是当我在浏览器中打开同一个页面时

false,Unknown-Username

这就是我想从代码中得到的。我是新来的,不知道为什么会这样。编辑:stacktrace

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)
t2a7ltrp

t2a7ltrp1#

与java.net类和实际浏览器相比,这与wire协议的工作方式有关。浏览器将比您使用的简单java.netapi复杂得多。
如果您想在java中获得等效的响应值,那么需要使用更丰富的httpapi。
此代码将给您与浏览器相同的响应;但是,您需要下载apachehttpjar组件
代码:

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
响应:[错误,未知用户名]

ou6hu8tu

ou6hu8tu2#

url返回状态码404,因此没有创建输入流(这里是轻度猜测),因此为空。对状态码进行排序,您应该没事。
用这个csv运行,结果很好:其他csv
如果错误代码对您很重要,则可以使用httpurlconnection:

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

这样,您就可以在进行快速if-then-else检查之前处理响应代码。

fgw7neuy

fgw7neuy3#

我在apachehttp库中尝试过。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端点的错误。您可以在这里查看[ApacheHTTP组件]库。

相关问题