我正在尝试使用 https://us.mc-api.net/ 中的 API 进行项目,我已将其作为测试。
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)
URL 返回状态代码 404,因此输入流(此处轻微猜测)未创建,因此为 null。对状态代码进行排序,应该就可以了。
用这个 CSV 运行它,没问题:其他 csv
如果错误代码对您很重要,那么您可以使用 HttpURLConnection:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println("code:"+conn.getResponseCode());
通过这种方式,您可以在进行快速 if-then-else 检查之前处理响应代码。
我用 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] 库。
这与有线协议与 java.net 类和实际浏览器相比的工作方式有关。浏览器将比您使用的简单 java.net API 复杂得多。
如果你想在Java中获得等效的响应值,那么你需要使用更丰富的HTTP API。
此代码将为您提供与浏览器相同的响应;但是,您需要下载 Apache HttpComponents jars
代码:
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
响应:[错误,未知用户名]